15

What does $@ mean as a bash script function parameter?

Example:

function foo()
{
    echo "$@" 1>&2;
}
18

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.

10

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

0

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

  • 2
    No, it is an internal variable meaning all parameters given to a function. They are available as $1,$2 etc anyway, irrespective of $@. – terdon Dec 29 '13 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.