Claudius Hubig wrote:
> bash(1) does not appear to mention $@.
It does but unfortunately it mentions it as a variable named @ and not
as $@ making it difficult to search. This problem has been discussed
upstream for instance here:
http://lists.gnu.org/archive/html/bug-bash/2011-12/msg00097.html
Maybe one day it will have a $@ together so that it can be found
easier. I know I voted to include it.
The documentation lists them like this:
The special parameters * and @ have special meaning when in
double quotes (see PARAMETERS below).
...
Special Parameters
The shell treats several parameters specially. These parameters may
only be referenced; assignment to them is not allowed.
* 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 in general if you want to pass arguements through then you want to
always use "$@". If you want all of the arguments to be part of one
single string then use "$*". Always using one of the quoted forms is
almost always better than using the unquoted form. When unquoted the
$* and $@ are equivelent.
Historical trivia: In the Bourne shell "$@" was documented to be the
same as "" if there weren't any arguments. In hindsight that was
undesirable since it was almost never what was wanted. It just fell
out of the normal use of quoting on a variable. If you said "$foo"
and foo was empty then you also got an empty "" string. So the same
thing happened for "$@". But "$@" was already special and an empty
string there is not a useful behavior. It required ${1+"$@"} to be
used to get the desired behavior.
${parameter+word}
Use Alternate Value. If parameter is unset, nothing is
substituted, otherwise the expansion of word is
substituted.
That idiom ${1+"$@"} was often seen in scripts meaning that if $1 was
set then replace the combination (due to the + action) with "$@". If
$1 was empty then it avoided getting an empty string. If $1 had
something in it then "$@" was guarenteed not to be the empty string.
All modern shells have the new behavior.
But this isn't so historical. To be portable to Sun's /bin/sh that
idiom is still needed. And so if you ever see programs designed to be
portable to those systems that idiom is still useful to know about.
Bob
Attachment:
signature.asc
Description: Digital signature