Friday, September 16, 2005

command line arguments

"$@" produces the list of positional paramters (command line arguments).

$@ and $* are similar, but behave differently in some cases:

echo 'for word in "$@"; do echo "<$word>"; done' > /tmp/trash
sh /tmp/trash "A couple of sins" "A spaced sentence" some single
words

produces

<A couple of sins>
<A spaced sentence>
<some>
<single>
<words>

but if you replace "$@" with "$*" you get

<A couple of sins A spaced sentence some single words>

and if you drop the quotation marks, as in "for words in $*; do..."

<A>
<couple>
<of>
<sins>
<A>
<spaced>
<sentence>
<some>
<single>
<words>

Without the quotation marks it does not matter if you use $* or $@.

0 Comments:

Post a Comment

<< Home