linuxbashshellunixsh

What does $@ mean in a shell script?


What does a dollar sign followed by an at-sign (@) mean in a shell script?

For example:

umbrella_corp_options $@

Solution

  • $@ is nearly the same as $*, both meaning "all command line arguments". They are often used to simply pass all arguments to another program (thus forming a wrapper around that other program).

    The difference between the two syntaxes shows up when you have an argument with spaces in it (e.g.) and put $@ in double quotes:

    wrappedProgram "$@"
    # ^^^ this is correct and will hand over all arguments in the way
    #     we received them, i. e. as several arguments, each of them
    #     containing all the spaces and other uglinesses they have.
    wrappedProgram "$*"
    # ^^^ this will hand over exactly one argument, containing all
    #     original arguments, separated by single spaces.
    wrappedProgram $*
    # ^^^ this will join all arguments by single spaces as well and
    #     will then split the string as the shell does on the command
    #     line, thus it will split an argument containing spaces into
    #     several arguments.
    

    Example: Calling

    wrapper "one two    three" four five "six seven"
    

    will result in:

    "$@": wrappedProgram "one two    three" four five "six seven"
    "$*": wrappedProgram "one two    three four five six seven"
                                 ^^^^ These spaces are part of the first
                                      argument and are not changed.
    $*:   wrappedProgram one two three four five six seven