Special Shell Variables

The Bourne shell has a number of special “automatic” variables that it maintains for informational purposes. These variables provide information such as the process ID of the shell, the exit status of the last command, and so on. This section provides a list of these special variables. For additional variables supported by specific Bourne shell variants such as BASH and ZSH, see the bash and zshparam manual pages, respectively.

Table B-1  Special shell variables

Variable

Description

Process information

$$

Process ID of shell

$PPID

Process ID of shell’s parent process.

Quirk Warning:For subshells, the value of PPID is inherited from the parent shell. Thus, PPID is only the parent of the outermost shell process.

$?

Exit status of last command.

$_

Name of last command.

$!

Process ID of last process run in the background using ampersand (&) operator. This is commonly used in conjunction with the wait builtin.

$PATH

A colon-delimited list of locations where trusted executables are installed. Any executable in one of these locations can be executed without specifying a complete path.

Field and record parsing

$IFS

Input Field Separators (uses are explained in “Variable Expansion and Field Separators”)

User information

$HOME

The user’s home directory.

$UID

The user’s ID.

Security Warning:This value can be modified by the calling script, so it should not be used for authentication purposes.

$USER

The user’s (short) login name.

Security Warning:This value can be modified by the calling script, so it should not be used for authentication purposes.

Miscellaneous Variables

$#

Number of arguments passed to the shell. This variable is described further in “Handling Flags and Arguments.”

$@

Complete list of arguments passed to the shell, separated by spaces.. This variable is described further in “Handling Flags and Arguments.”

$*

Complete list of arguments passed to the shell, separated by the first character of the IFS (input field separators) variable. This variable is described further in “Handling Flags and Arguments.”

$-

A list of all shell flags currently enabled.

$PWD

The current working directory. Equivalent to executing the pwd command.