In a bash script, I need to pass a parameter to another program. The parameter has spaces in so must be quoted. Everything works fine in this simple case 1:
/bin/echo /some/command --param="abc def ghi"
Output:
/some/command --param=abc def ghi
The problem begins if I want to make the bash script more sophisticated, somehow the parameter value has changed in case 2 and 3:
FOO="ghi"
DEFAULTS="--param=\"abc def $FOO\""
/bin/echo /some/command $DEFAULTS
DEFAULTS='--param="abc def '$FOO'"'
/bin/echo /some/command $DEFAULTS
Output:
/some/command --param="abc def ghi"
/some/command --param="abc def ghi"
The double quotes surrounding abc def ghi
are shown in 2 and 3, whereas they are not shown for 1.
This is better illustrated by having /some/command actually print what it receives as the first parameter. Here the whole string "abc def ghi"
is received as the value of the param
parameter:
perl -le'print $ARGV[0]' param="abc def ghi"
Output (right):
param=abc def ghi
But here only "abc
is received, with one leading quote:
DEFAULTS='param="abc def ghi"'
perl -le'print $ARGV[0]' $DEFAULTS
DEFAULTS="param=\"abc def ghi\""
perl -le'print $ARGV[0]' $DEFAULTS
Output (wrong):
param="abc
param="abc
How do I use bash to dynamically construct a string like param="abc def ghi"
and be able to pass it intact to another command?