Special parameters
Special parameters are predefined parameters that have symbolic names and provide specific information about the shell environment. They are not user-defined variables and cannot be assigned values with the assignment syntax.
Below are the special parameters and their meanings:
-
@: All positional parameters.- Expands to all positional parameters as separate fields. Useful for passing all arguments as is to a utility or function.
- When expanded outside double quotes, the result is subject to field splitting and pathname expansion. To preserve each parameter as a separate field, use
"$@". If there are no positional parameters,"$@"expands to zero fields. - In contexts where only one field is expected (such as in the content of a here-document),
@expands to a single field with all positional parameters joined by the first character of theIFSvariable (defaults to space if unset, or no separator ifIFSis empty).
$ set foo 'bar bar' baz # three positional parameters $ for value in "$@"; do echo "[$value]"; done [foo] [bar bar] [baz] $ for value in $@; do echo "[$value]"; done [foo] [bar] [bar] [baz] -
*: All positional parameters.- Similar to
@, but in double quotes,*expands to a single field containing all positional parameters joined by the first character ofIFS.
$ set foo 'bar bar' baz # three positional parameters $ for value in "$*"; do echo "[$value]"; done [foo bar bar baz] $ for value in $*; do echo "[$value]"; done [foo] [bar] [bar] [baz] - Similar to
-
#: Number of positional parameters.$ set foo 'bar bar' baz $ echo "$#" 3 -
?: Exit status of the last command. -
-: Current shell options.- Expands to the short names of all currently set shell options, concatenated together. Options without a short name are omitted. For example, if
-iand-mare set, the value isim.
- Expands to the short names of all currently set shell options, concatenated together. Options without a short name are omitted. For example, if
-
$: Process ID of the current shell.- Set when the shell starts and remains constant, even in subshells.
-
!: Process ID of the last asynchronous command.- Updated when an asynchronous command is started or resumed in the background.
- Since 3.0.0, this parameter is unset if no asynchronous command has been started in the current shell environment. Previously, it was set to
0in such cases.
-
0: Name of the shell or script being executed.- Set at shell startup and remains constant.
- If neither the
-cnor-sshell option is active, the value of0is the first operand in the shell invocation (the script pathname). - If the
-coption is used and a second operand is present, that operand is used as0. - Otherwise,
0is set to the first argument passed to the shell, usually the shell’s name.