Jay Taylor's notes

back to listing index

What is $* and $# in Linux?

[web search]
Original source (superuser.com)
Tags: bash shell-scripting dollar superuser.com
Clipped on: 2018-01-05

What do the following environment variables in Linux mean?

  1. What is $* (dollar sign followed by an asterisk)?
  2. What is $# (dollar sign next to a hash mark/number sign/octothorpe/pound sign)?
Image (Asset 2/5) alt=

These are part of POSIX standard, and should be supported by all compliant shells. For the reference, below is POSIX standard definitions for each special parameter. Do note there's three additional variables: $-, $$ and $!.

$@:

Expands to the positional parameters, starting from one. When the expansion occurs within double-quotes, and where field splitting (see Field Splitting) is performed, each positional parameter shall expand as a separate field, with the provision that the expansion of the first parameter shall still be joined with the beginning part of the original word (assuming that the expanded parameter was embedded within a word), and the expansion of the last parameter shall still be joined with the last part of the original word. If there are no positional parameters, the expansion of '@' shall generate zero fields, even when '@' is double-quoted.

$*:

Expands to the positional parameters, starting from one. When the expansion occurs within a double-quoted string (see Double-Quotes), it shall expand to a single field with the value of each parameter separated by the first character of the IFS variable, or by a if IFS is unset. If IFS is set to a null string, this is not equivalent to unsetting it; its first character does not exist, so the parameter values are concatenated.

$#:

Expands to the decimal number of positional parameters. The command name (parameter 0) shall not be counted in the number given by '#' because it is a special parameter, not a positional parameter.

$?:

Expands to the decimal exit status of the most recent pipeline (see Pipelines).

$-:

(Hyphen.) Expands to the current option flags (the single-letter option names concatenated into a string) as specified on invocation, by the set special built-in command, or implicitly by the shell.

$$:

Expands to the decimal process ID of the invoked shell. In a subshell (see Shell Execution Environment ), '$' shall expand to the same value as that of the current shell.

$!:

Expands to the decimal process ID of the most recent background command (see Lists) executed from the current shell. (For example, background commands executed from subshells do not affect the value of "$!" in the current shell environment.) For a pipeline, the process ID is that of the last command in the pipeline.

$0:

(Zero.) Expands to the name of the shell or shell script. See sh for a detailed description of how this name is derived.

Hot Network Questions

site design / logo © 2017 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2017.12.22.28256