Parameter and variable expansion

From LQWiki
Jump to navigation Jump to search

The current values of bash variables and command line parameters are indicated by the variable name or parameter number preceded by $. Thus $configfile is the current value of the variable called configfile and $2 is the current value of the second argument on the command line. The name or number can be enclosed in braces where this is necessary to avoid confusion. Braces are required for command line parameters with two-digit numbers and for variable names that are followed immediately (i.e. without an intervening space) by characters that are not part of the name.

Bash automatically expands all such parameters and variables by substituting their current values.

Bash recognises a number of "special" variables which are expanded as follows:

  • $0 expands to the name of the script.
  • $$ expands to the pid of the script.
  • $* expands to the complete list of command line arguments.
  • $# expands to the number of command line arguments.
  • $? expands to the return value of the previous command.