How to pass quoted parameters to another program

2019-03-01 07:39发布

问题:

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?

回答1:

Use an array; this is why they were introduced.

FOO="ghi"
# Contains one element, but not the literal quotes
DEFAULTS=(--param="abc def $FOO")
# command gets 1 argument, the single element of the array
/some/command "${DEFAULTS[@]}"

# Two elements
DEFAULTS=(--param="abc def $FOO" --other="foo bar")
# command gets 2 arguments
/some/command "${DEFAULTS[@]}"


回答2:

You are almost there. You just have to put the $DEFAULTS into double quotes so that bash doesn't do the word spliting.

DEFAULTS='param="ac def ghi"'
perl -le'print $ARGV[0]' "$DEFAULTS"

Output:

param="ac def ghi"