Special variables in bash
When you call a shell script from the command line, you can pass it additional arguments called Positional Parameters. For example, when entering the following in the command line to run the script myscript.sh
:
./myscript.sh param1 param2
The variables would have the following values:
"$0" = "./myscript.sh"
"$1" = "param1"
"$2" = "param2"
The variable $#
gives the number of parameters, not including the command. In this example:
"$#" = 2
The variable $*
lists all the parameters as a single word (quotes added to emphasize string boundaries):
"$*" = "param1 param2"
The variable $@
lists each parameter as a separate quoted string (quotes added to emphasize string boundaries):
"$@" = "param1" "param2"
Note that you should almost always quote variables in bash, so you should use "$@"
and not $@
. See this programming guide for more information.
The variables $*
& $@
are the arguments passed when invoking the script (or function, if inside a function).
"$@"
is the one you should normally use: it expands to separate words, one-for-one for the arguments that are passed in.
"$*"
expands to a single "word" consisting of all the arguments separated by spaces.
(Don't ever use them without quotes; that results in word-splitting and general messiness you don't want to deal with.)
$#
is the number of arguments (and since it's always a digit string, it doesn't really matter whether you quote it)
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…", where c 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.
# Expands to the number of positional parameters in decimal.
$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.
Source: https://www.gnu.org/software/bash/manual/html_node/index.html