What does $@ mean as a bash script function parameter

The $@ 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

Tags:

Shell

Bash