Reputation: 10597
The following code
myopts=( --url="http://www.lemonde.fr" --out="hello world" --out-format="png" )
echo "going to execute following command: cutycapt ${myopts[@]}"
# commented out because this way you do not have to have cutycapt to test
# cutycapt "${myopts[@]}"
outputs the following:
going to execute following command: cutycapt --url=http://www.lemonde.fr --out=hello world --out-format=png
but really what is executed is:
cutycapt --url="http://www.lemonde.fr" --out="hello world" --out-format="png"
Thus, that is what I would like to be output from the echo
command.
Upvotes: 2
Views: 56
Reputation: 14510
You can use printf
with the %q
format string to print a quoted version of the arguments like
printf '%q ' cutycapt "${myopts[@]}"; printf '\n'
(we add our \n
to the end since printf
doens't automatically add one)
Example:
$ myopts=( --url="http://www.lemonde.fr" --out="hello world" --out-format="png" )
$ printf 'going to execute following command: '; printf '%q ' cutycapt "${myopts[@]}"; printf '\n'
going to execute following command: cutycapt --url=http://www.lemonde.fr --out=hello\ world --out-format=png
with the escape of the space in hello world
Depending on why you want this, have you considered just running with set -x
enabled, so you see the expanded command line before it's executed anyway?
$ set -x
$ cutycapt "${myopts[@]}"
+ cutycapt --url=http://www.lemonde.fr '--out=hello world' --out-format=png
Upvotes: 2
Reputation: 52261
When you create your array, its elements are subject to quote removal. If you want to keep them, you have to add them quoted already:
$ myopts=( '--url="http://www.lemonde.fr"' '--out="hello world"' '--out-format="png"' )
$ echo "going to execute following command: cutycapt ${myopts[@]}"
going to execute following command: cutycapt --url="http://www.lemonde.fr" --out="hello world" --out-format="png"
If you look at your array elements after creating myopts
, you'll see that they're already missing the quotes:
$ myopts=( --url="http://www.lemonde.fr" --out="hello world" --out-format="png" )
$ (IFS=$'\n'; echo "${myopts[*]}")
--url=http://www.lemonde.fr
--out=hello world
--out-format=png
So it's not the expansion that removes them – it happens when assigning.
Upvotes: 3