What does $ @ mean in a shell script?

What does the dollar sign mean, followed by the at ( @ ) sign in the shell script?

For example:

 umbrella_corp_options $@ 
+141
linux unix bash shell sh
Apr 03 2018-12-12T00:
source share
5 answers

$@ is all the parameters passed to the script.

For example, if you call ./someScript.sh foo bar then $@ will be equal to foo bar .

If you do:

 ./someScript.sh foo bar 

and then inside the link to someScript.sh :

 umbrella_corp_options "$@" 

this will be passed to umbrella_corp_options with each individual parameter enclosed in double quotes, which allows you to receive parameters with a space at the caller and pass them on.

+190
Apr 03 '12 at 13:30
source share

$@ is almost the same as $* , which means "all command line arguments." They are often used to simply pass all arguments to another program (thus forming a wrapper around this other program).

The difference between these two syntaxes is manifested when you have an argument with spaces in it (for example), and you put $@ in double quotes:

 wrappedProgram "$@" # ^^^ this is correct and will hand over all arguments in the way # we received them, ie 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: Call

 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 
+86
Apr 3 2018-12-12T00:
source share

These are command line arguments, where:

$@ = stores all arguments in a string list
$* = stores all arguments as a single line
$# = stores the number of arguments

+28
Oct 22 '13 at 15:21
source share

Using pure $@ means that in most cases "it hurts the programmer as hard as you can," because in most cases it leads to problems with word separation and spaces and other characters in the arguments.

In (guessed) 99% of all cases, you need to enclose it in " : "$@" - this is what can be used to reliably iterate over the arguments.

 for a in "$@"; do something_with "$a"; done 
+9
Apr 3 2018-12-12T00:
source share

From the manual:

@

Expands to positional parameters, starting with one. When expansion occurs in double quotes, each parameter expands to a single word. That is, "$ @" is equivalent to "$ 1" "$ 2" .... If double quotation decomposition occurs inside a word, the decomposition of the first parameter is connected to the initial part of the original word, and the extension of the last parameter is connected to the last part of the original word. When there are no positional parameters, "$ @" and $ @ expand to zero (that is, they are deleted).

+6
Apr 03 2018-12-12T00:
source share



All Articles