Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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