shell – all parameters – $* vs $@

19 Jul 2011

Both $* and $@ expand to “all positional parameters”; in a nutshell – ”$@” is often desired as it handles spaces correctly; see man bash “Special Parameters”.

% ls my*     
myfoo  my foo  myqux

----------------

% ./star_test my*
$*
myfoo
my     :-(
foo    :-(
myqux

$@
myfoo
my     :-(
foo    :-(
myqux

"$*"
myfoo my foo myqux    <-- probably not desired :-|

"$@"
myfoo
my foo  :-)
myqux

----------------

% cat star_test
#!/bin/bash
echo '$*'
./pargs $*
echo

echo '$@'
./pargs $@
echo

echo '"$*"'
./pargs "$*"
echo

echo '"$@"'
./pargs "$@"

----------------

% cat pargs
#!/bin/bash
for i ; do
    echo $i
done
comments powered by Disqus

  « Previous: Next: »