Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bash script that is a simple wrapper around another process:

$ cat ~/bin/s3cmd
#!/bin/sh
trickle -u 80 ~/bin/s3cmd.py $*
$

This works great when the parameters don't contain spaces. However, it seems to fail whenever the parameters passed to the outer s3cmd script contain spaces, even if those are escaped on the command line.

How do I whitespace-proof this, such that each single parameter is passed on properly to s3cmd.py?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

Use "$@".

#!/bin/sh
trickle -u 80 ~/bin/s3cmd.py "$@"

There is no functional difference between $* and $@ is when undergoing implicit word splitting (almost always when unquoted). When quoted, however, $* is a single string separated by the first character of IFS, and "$@" is a real array (what you want).

$ set foo bar baz
$ IFS=c
$ printf '%s\n' "$@"
foo
bar
baz
$ printf '%s\n' "$*"
foocbarcbaz
share|improve this answer
2  
Note that there can be differences between $* unquoted and $@ unquoted under some circumstances with some shells. See for instance set a b; IFS=; a=$@; b=$*; echo "$a,$b" in bash or AT&T ksh. Also note that in the Bourne shell (but who cares about the Bourne shell nowadays) SPC is used to separate items in "$*", not the first character of $IFS. –  Stephane Chazelas Nov 13 '12 at 21:09
1  
@StephaneChazelas - Perhaps I should have said "when undergoing implicit word splitting" rather than "when unquoted". –  Chris Down Nov 13 '12 at 21:10
add comment

You'll want to use $@ instead of $* and quote it:

trickle -u 80 ~/bin/s3cmd.py "$@"

See also the Special Parameters section in the bash manpage.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.