Say I have some command:

somecommand "$@"

What is the significance of $@? It must be some unix trickery that I'm not familiar with. Unfortunately, since its all punctuation, I can't google it either.

link|improve this question

71% accept rate
feedback

1 Answer

up vote 5 down vote accepted

It's the current shell script's or function's parameters, individually quoted.

man bash says:

@ 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" ...


Given the following script:

#!/usr/bin/env bash

function all_args {
    # repeat until there are no more arguments
    while [ $# -gt 0 ] ; do
        # print first argument to the function
        echo $1
        # remove first argument, shifting the others 1 position to the left
        shift
    done
}

echo "Quoted:"
all_args "$@"
echo "Unquoted:"
all_args $@

This happens when executed:

$ ./demo.sh foo bar "baz qux"
Quoted:
foo
bar
baz qux
Unquoted:
foo
bar
baz
qux
link|improve this answer
You can btw find it by searching for \$@ in man bash. You need to escape the dollar character. – Daniel Beck Nov 28 '11 at 19:02
feedback

Your Answer

 
or
required, but never shown

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