Problem
In a shell script, what does a dollar sign followed by an at-symbol (@) mean?
For example:
umbrella_corp_options $@
Asked by trusktr
Solution #1
All of the arguments supplied to the script are represented by $@.
If you run./someScript.sh foo bar, for example, $@ will be identical to foo bar.
If you do:
./someScript.sh foo bar
Then, inside someScript.sh, there’s a reference to:
umbrella_corp_options "$@"
This will be supplied to umbrella corp options with each each argument encased in double quotes, allowing the caller to pass parameters with blank spaces.
Answered by Har
Solution #2
$@ and $* are similar in that they both mean “all command line arguments.” They’re frequently used to send all arguments to another application (thus forming a wrapper around that other program).
When you have an argument with spaces in it (e.g.) and place $@ in double quotes, the difference between the two syntaxes becomes apparent:
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
Answered by Alfe
Solution #3
The command line arguments are as follows:
$@ = saves all of the arguments in a string list. $* = collects all arguments into a single string. $# = keeps track of how many arguments there are.
Answered by Sameer Duwal
Solution #4
In most circumstances, using a pure $@ implies “hurt the programmer as much as you can,” because it causes problems with word separation, spaces, and other characters in arguments.
It is needed to enclose it in “: “$@” is what can be used to reliably iterate over the arguments in (guessed) 99 percent of all circumstances.
for a in "$@"; do something_with "$a"; done
Answered by glglgl
Solution #5
In brief, $@ expands to the arguments passed from the caller to a function or a script. Its meaning varies depending on the situation: Inside a function, it expands to the arguments passed to such function. If used in a script (outside a function), it expands to the arguments passed to such script.
$ cat my-script
#! /bin/sh
echo "$@"
$ ./my-script "Hi!"
Hi!
$ put () { echo "$@"; }
$ put "Hi!"
Hi!
* Please take note of the word splitting.
The shell splits tokens based on the IFS environment variable’s contents. The default value for this property is tn, which stands for whitespace, tab, and newline. When you expand “$@,” you get a clean copy of the parameters supplied. It’s possible that expanding $@ won’t work. Any arguments containing characters found in IFS may be divided into two or more arguments, or they may be truncated.
As a result, you’ll want to utilize “$@” rather than “$@” most of the time.
Answered by Luis Lavaire.
Post is based on https://stackoverflow.com/questions/9994295/what-does-mean-in-a-shell-script