2

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?

2 Answers 2

6

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
2
  • 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. Nov 13, 2012 at 21:09
  • 1
    @StephaneChazelas - Perhaps I should have said "when undergoing implicit word splitting" rather than "when unquoted".
    – Chris Down
    Nov 13, 2012 at 21:10
4

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .