Jump to content

Built-in bash Shell Variables

0
  adfm's Photo
Posted May 14 2010 04:05 PM

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:

$#

Number of command-line arguments.

$-

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.

$0

First word; that is, the command name. This will have the full pathname if it was found via a PATH search.

{:content:}lt;/code>n

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 ${n}.

$*, $@

All arguments on command line ($1 $2 …).

"$*"

All arguments on command line as one string ("$1 $2…"). The values are separated by the first character in $IFS.

"$@"

All arguments on command line, individually quoted ("$1" "$2" …).

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 MAIL file during mail checks.

BASH

The full pathname used to invoke this instance of Bash.

BASHOPTS

A read-only, colon-separated list of shell options that are currently enabled. Each item in the list is a valid option for shopt -s. If this variable exists in the environment when Bash starts up, it sets the indicated options before executing any startup files.

BASHPID

The process ID of the current Bash process. In some cases, this can differ from $.

BASH_ALIASES

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.

BASH_ARGC

Array variable. Each element holds the number of arguments for the corresponding function or dot-script invocation. Set only in extended debug mode, with shopt -s extdebug. Cannot be unset.

BASH_ARGV

An array variable similar to BASH_ARGC. Each element is one of the arguments passed to a function or dot-script. It functions as a stack, with values being pushed on at each call. Thus, the last element is the last argument to the most recent function or script invocation. Set only in extended debug mode, with shopt -s extdebug. Cannot be unset.

BASH_CMDS

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.

BASH_COMMAND

The command currently executing or about to be executed. Inside a trap handler, it is the command running when the trap was invoked.

BASH_EXECUTION_STRING

The string argument passed to the -c option.

BASH_LINENO

Array variable, corresponding to BASH_SOURCE and FUNCNAME. For any given function number i (starting at zero), ${FUNCNAME[i]} was invoked in file ${BASH_SOURCE[i]} on line ${BASH_LINENO[i]}. The information is stored with the most recent function invocation first. Cannot be unset.

BASH_REMATCH

Array variable, assigned by the =~ operator of the [[ ]] construct. Index zero is the text that matched the entire pattern. The other indices are the text matched by parenthesized subexpressions. This variable is read-only.

BASH_SOURCE

Array variable, containing source filenames. Each element corresponds to those in FUNCNAME and BASH_LINENO. Cannot be unset.

BASH_SUBSHELL

This variable is incremented by one each time a subshell or subshell environment is created.

BASH_VERSINFO[0]

The major version number, or release, of Bash.

BASH_VERSINFO[1]

The minor version number, or version, of Bash.

BASH_VERSINFO[2]

The patch level.

BASH_VERSINFO[3]

The build version.

BASH_VERSINFO[4]

The release status.

BASH_VERSINFO[5]

The machine type; same value as in $MACHTYPE.

BASH_VERSION

A string describing the version of Bash.

COMP_CWORD

For programmable completion. Index into COMP_WORDS, indicating the current cursor position.

COMP_KEY

For programmable completion. The key, or final key in a sequence, that caused the invocation of the current completion function.

COMP_LINE

For programmable completion. The current command line.

COMP_POINT

For programmable completion. The position of the cursor as a character index in $COMP_LINE.

COMP_TYPE

For programmable completion. A character describing the type of programmable completion. The character is one of Tab for normal completion, ? for a completions list after two Tabs, ! for the list of alternatives on partial word completion, @ for completions if the word is modified, or % for menu completion.

COMP_WORDBREAKS

For programmable completion. The characters that the readline library treats as word separators when doing word completion.

COMP_WORDS

For programmable completion. Array variable containing the individual words on the command line.

COPROC

Array variable that holds the file descriptors used for communicating with an unnamed coprocess. For more information, see the section called “Coprocesses”.

DIRSTACK

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.

EUID

Read-only variable with the numeric effective UID of the current user.

FUNCNAME

Array variable, containing function names. Each element corresponds to those in BASH_SOURCE and BASH_LINENO.

GROUPS

Array variable, containing the list of numeric group IDs in which the current user is a member.

HISTCMD

The history number of the current command.

HOSTNAME

The name of the current host.

HOSTTYPE

A string that describes the host system.

LINENO

Current line number within the script or function.

MACHTYPE

A string that describes the host system in the GNU cpu-company-system format.

MAPFILE

Default array for the mapfile and readarray commands. See the entry for mapfile for more information.

OLDPWD

Previous working directory (set by cd).

OPTARG

Value of argument to last option processed by getopts.

OPTIND

Numerical index of OPTARG.

OSTYPE

A string that describes the operating system.

PIPESTATUS

Array variable, containing the exit statuses of the commands in the most recent foreground pipeline.

PPID

Process number of this shell’s parent.

PWD

Current working directory (set by cd).

RANDOM[=n]

Generate a new random number with each reference; start with integer n, if given.

READLINE_LINE

For use with bind -x. The contents of the editing buffer are available in this variable.

READLINE_POINT

For use with bind -x. The index in $READLINE_LINE of the insertion point.

REPLY

Default reply; used by select and read.

SECONDS[=n]

Number of seconds since the shell was started, or, if n is given, number of seconds since the assignment + n.

SHELLOPTS

A read-only, colon-separated list of shell options (for set -o). If set in the environment at startup, Bash enables each option present in the list before reading any startup files.

SHLVL

Incremented by one every time a new Bash starts up.

UID

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”).

Cover of bash Pocket Reference
Learn more about this topic from bash Pocket Reference. 

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.

Learn More Read Now on Safari


Tags:
0 Subscribe


0 Replies