tags:

views:

582

answers:

2

I'm trying to get quoted parameters of a bash script to safely be received by a nested script. Any ideas?

test.sh

#!/bin/bash
echo $*
bash myecho.sh $*

myecho.sh

#!/bin/bash
 echo $1
 echo $2
 echo $3
 echo $4

Sample:

bash test.sh aaa bbb '"ccc ddd"'

Result:

aaa bbb "ccc ddd"
aaa
bbb
"ccc
ddd"

Wanted result

aaa bbb "ccc ddd"
aaa
bbb
ccc ddd
+10  A: 
#!/bin/bash
echo $*
bash myecho.sh "$@"

Note the "$@" construct is not bash specific and should work with any POSIX shell (it does with dash at least). Note also that given the output you want, you don't need the extra level of quoting at all. I.E. just call the above script like:

./test.sh 1 2 "3 4"
pixelbeat
"$@" works with any Bourne shell or Bourne shell derivative (from 1978 onwards), including Korn and Bash. Probably 95% of the time, using "$@" is correct and $* is wrong.
Jonathan Leffler
+1  A: 

From the Bash man page (http://linux.die.net/man/1/bash):

$* -- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

$@ -- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

...

So, $* and $@ are identical unless they are inside double quotes ("), in which case, 99% of the time you probably want $@.

for example:


# cat params.sh
echo '$1=' $1
echo '$2=' $2
echo '$3=' $3
echo '$4=' $4
# ./params.sh aaa "bb bb" '"ccc ddd"'
$1= aaa
$2= bb bb
$3= "ccc ddd"
$4=

# cat dollar-star.sh
./params.sh $*
# ./dollar-star.sh aaa "bb bb" '"ccc ddd"'
$1= aaa
$2= bb
$3= bb
$4= "ccc

# cat dollar-at.sh
./params.sh $@
# ./dollar-at.sh aaa "bb bb" '"ccc ddd"'
$1= aaa
$2= bb
$3= bb
$4= "ccc

# cat quote-dollar-star.sh
./params.sh "$*"
# ./quote-dollar-star.sh aaa "bb bb" '"ccc ddd"'
$1= aaa bb bb "ccc ddd"
$2=
$3=
$4=

# cat quote-dollar-at.sh
./params.sh "$@"
# ./quote-dollar-at.sh aaa "bb bb" '"ccc ddd"'
$1= aaa
$2= bb bb
$3= "ccc ddd"
$4=

Naked $* and $@ both strip one level of escaping off the arguments.

"$*" smashes all the arguments into a single space delimited string (rarely the intended effect)

"$@" passes the arguments to the child exactly as they were to the parent. Even handles double quotes and other dangerous characters inside the arguments

ddopson