I reckon that the handle $@
in a shell script is an array of all arguments given to the script. Is this true?
I ask because I normally use search engines to gather information, but I can't google for $@
and I have grown too accustomed to easily getting served everything.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
$@ is basically use for refers all the command-line arguments of shell-script. $1 , $2 , $3 refer to the first command-line argument, the second command-line argument, third argument. Follow this answer to receive notifications.
$@ refers to all of a shell script's command-line parameters. $1 , $2 , etc., refer to the first command-line parameter, the second command-line parameter, etc. Place variables in quotes if the values might have spaces in them.
Difference between “$0” and “$@” in Unix shell scripts.. They are entirely different. $0 is the name of the script; "$@" expands to the command-line arguments.
Yes. Please see the man page of Bash (the first thing you go to) under Special Parameters:
Special Parameters
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
*
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is,"$*"
is equivalent to"$1c$2c..."
, wherec
is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
@
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"
... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters,"$@"
and$@
expand to nothing (i.e., they are removed).
Just from reading that I would have never understood that "$@"
expands into a list of separate parameters. Whereas, "$*"
is one parameter consisting of all the parameters added together.
If it still makes no sense, do this.
Bash special parameters explained with four example shell scripts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With