bash has long been the go-to shell for quick one-off scripts. If you're in the midst of writing such a script and could use a handy reference of the built-in shell variables, than this excerpt from Arnold Robbins' bash Pocket Reference will do the trick.
Built-in variables are automatically set by the shell and
are typically used inside shell scripts. Built-in variables can make use
of the variable substitution patterns shown previously. Note that the
{:content:}lt;/code> is not actually part of the
variable name, although the variable is always referenced this way. The
following are available in any Bourne-compatible shell:
| |
| Options currently in effect (supplied on command line or to set). The shell sets some options automatically. |
| Exit value of last executed command. |
| Process number of the shell. |
| Process number of last background command. |
| First word; that is, the command
name. This will have the full pathname if it was found via a
|
| Individual arguments on command line
(positional parameters). The Bourne shell allows only nine
parameters to be referenced directly (n =
1–9); Bash allows n to be greater than 9 if
specified as |
| All arguments on command line
( |
| All arguments on command line as one
string ( |
| All arguments on command line,
individually quoted ( |
Bash automatically sets the following additional variables:
| Temporary variable; initialized to
pathname of script or program being executed. Later, stores the
last argument of previous command. Also stores name of matching
|
The full pathname used to invoke this instance of Bash. | |
A read-only, colon-separated list of
shell options that are currently enabled. Each item in the list
is a valid option for | |
The process ID of the current Bash
process. In some cases, this can differ from
| |
Associative array variable. Each element holds an alias defined with the alias command. Adding an element to this array creates a new alias; removing an element removes the corresponding alias. | |
Array variable. Each element holds
the number of arguments for the corresponding function or
dot-script invocation. Set only in extended debug mode, with
| |
An array variable similar to | |
Associative array variable. Each element refers to a command in the internal hash table maintained by the hash command. The index is the command name and the value is the full path to the command. Adding an element to this array adds a command to the hash table; removing an element removes the corresponding entry. | |
The command currently executing or about to be executed. Inside a trap handler, it is the command running when the trap was invoked. | |
The string argument passed to the
| |
Array variable, corresponding to
| |
Array variable, assigned by the
| |
Array variable, containing source
filenames. Each element corresponds to those in | |
This variable is incremented by one each time a subshell or subshell environment is created. | |
The major version number, or release, of Bash. | |
| The minor version number, or version, of Bash. |
| The patch level. |
| The build version. |
| The release status. |
| The machine type; same value as in
|
A string describing the version of Bash. | |
For programmable completion. Index
into | |
For programmable completion. The key, or final key in a sequence, that caused the invocation of the current completion function. | |
For programmable completion. The current command line. | |
For programmable completion. The
position of the cursor as a character index in | |
For programmable completion. A
character describing the type of programmable completion. The character is one
of Tab for normal completion, | |
For programmable completion. The characters that the readline library treats as word separators when doing word completion. | |
For programmable completion. Array variable containing the individual words on the command line. | |
| Array variable that holds the file descriptors used for communicating with an unnamed coprocess. For more information, see the section called “Coprocesses”. |
Array variable, containing the contents of the directory stack as displayed by dirs. Changing existing elements modifies the stack, but only pushd and popd can add or remove elements from the stack. | |
Read-only variable with the numeric effective UID of the current user. | |
Array variable, containing function
names. Each element corresponds to those in | |
Array variable, containing the list of numeric group IDs in which the current user is a member. | |
The history number of the current command. | |
The name of the current host. | |
A string that describes the host system. | |
Current line number within the script or function. | |
A string that describes the host system in the GNU cpu-company-system format. | |
Default array for the mapfile and readarray commands. See the entry for mapfile for more information. | |
Previous working directory (set by cd). | |
Value of argument to last option processed by getopts. | |
Numerical index of | |
A string that describes the operating system. | |
Array variable, containing the exit statuses of the commands in the most recent foreground pipeline. | |
Process number of this shell’s parent. | |
Current working directory (set by cd). | |
Generate a new random number with each reference; start with integer n, if given. | |
For use with | |
For use with | |
Default reply; used by select and read. | |
| Number of seconds since the shell was started, or, if n is given, number of seconds since the assignment + n. |
A read-only, colon-separated list of
shell options (for | |
Incremented by one every time a new Bash starts up. | |
Read-only variable with the numeric real UID of the current user. |
Many of these variables provide support for either the Bash Debugger (see http://bashdb.sourceforge.net) or for programmable completion (see the section the section called “Programmable Completion”).
You need to know how to work with the bash shell if you want to get to the heart of Unix systems, including Linux and Mac OS X. Now covering the most recent version of bash, this concise little book puts all of the essential information about bash at your fingertips. You'll quickly find answers to annoying questions that always come up when you're writing shell scripts -- What characters do you need to quote? How do you get variable substitution to do exactly what you want? How do you use arrays? -- and much more.




Help









