Bash: optionally passing arguments to a command
By John Campbell •
I'm trying to add arguments to a command call depend on another variable. Please look at the shell scripting code:
curl \ $([ -z "${title}" ] || echo --data-urlencode title=${title}) \ In the example, if title is given not null, an argument will be added to curl.
This does not work correctly if title contains spaces. Also I couldn't surround $(...) with quotations, because if title is null, it will yield an unexpected empty argument to curl.
What should I do to make it work as expect.
1 Answer
I've solved the problem with the bash ${var:+...} syntax, (reference).
The script now changes to
curl \ ${title:+ --data-urlencode "title=${title}"} \ which works perfectly.
Also see: