What does $@
mean as a bash script function parameter?
Example:
function foo()
{
echo "$@" 1>&2;
}
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityThe $@
variable expands to all the parameters used when calling the function, so
function foo()
{
echo "$@"
}
foo 1 2 3
would display 1 2 3
. If not used inside a function, it specifies all parameters used when calling the script. See the bash manual page for more info.
$@ is one of the two "positional parameter" representions in bash, the other being $*.
Both, $@ and $* are internal bash variables that represents all parameters passed into a function or a script, with one key difference, $@ has each parameter as a separate quoted string, whereas $* has all parameters as a single string. That difference is shown in the following code:
foo() {
while [ "$1" != "" ]; do
echo $1
shift
done
}
dollar_at () {
foo "$@"
}
dollar_star () {
foo "$*"
}
echo "Using \$@"
dollar_at a b c
echo "Using \$*"
dollar_star a b c
Output:
Using $@
a
b
c
Using $*
a b c
Note, when called with $*, exactly one argument is passed to foo(), but with $@ three arguments are passed to foo().
More info: http://tldp.org/LDP/abs/html/internalvariables.html#APPREF
I expect it means take as a hash all input parameters. So if there are 3 input parameters they will be available within the function in the form of $1, $2, $3
$1
,$2
etc anyway, irrespective of $@
.