0

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.

5

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
  • 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

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.