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}) \
http://example.com
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.
Best Answer
I've solved the problem with the bash
${var:+...}
syntax, (reference).The script now changes to
which works perfectly.
Also see: