Bash Shell Variables

This is a list of special variables that are set in bash(1) to configure it's behavior, or by bash(1) to indicate state. For programming variables, including special ones, see here.


Bash

$BASHPID is the current shell's PID.

$BASHOPTS is a colon-delimited list of enabled shell options.

$BASH_COMPAT is the compat level of the shell. Setting to 4.2 or 42 is equivalent to using the compat42 [[Bash/ShellOption#Compatibility_Mode|shell option.

$BASH_ALIASES is an associative array of aliases. $BASH_CMDS is an associative array of builtins.

$_ is, at first, the pathname used to call the shell. Thereafter every time a command is called, $_ is the last argument to that command. Furthermore, while a command is executed, $_ is set to the pathname used to call that command. This is exposed when a command inspects its environment.

$BASH_COMMAND is the command currently executing. If bash(1) is executing a trap, $BASH_COMMAND will show the command that triggered the trap.

$BASH_ARGV is an array containing all the arguments in the current shell call stack, while $BASH_ARGC is the count of those arguments. These are only set if extended debugging mode is on.

In a similar manner, $BASH_ARGV0 is the name of the shell. It is identical to $0.

BASH_ENV

BASH_EXECUTION_STRING

BASH_LINENO

BASH_LOADABLES_PATH

BASH_REMATCH

BASH_SOURCE

BASH_SUBSHELL

BASH_VERSINFO

BASH_VERSION

BASH_XTRACEFD


Directory Stack

$DIRSTACK is an array containing the directory stack.


Get Opts

The getopts builtin sets two variables:


Home

$HOME is the user's home directory.


Inter Field Separator

$IFS defines the characters that should be interpreted as separating tokens.

Frequently, this is only manipulated when writing a pipeline that specially delimits items with the null byte.


Mail

If $MAIL is set, bash(1) monitors it (assuming it is a mail file or Maildir directory) and informs the user of changes.

Similarly, $MAILPATH is a colon-delimited list of locations to monitor. If set, $MAIL is not monitored.


Path

$PATH is a colon-delimited list of directories searched for commands.

$CDPATH is similarly searched by the cd builtin.


Prompts

$PS1 is printed at the beginning of every command prompt.

$PS2 is printed at the beginning of run-on lines.

$PS3 is printed at the prompt in select menus.


CategoryRicottone