Page Body

Important Shell Variables

There are many shell variables. Knowing about the most useful ones can help you better understand and customize your shell's environment.

Note: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.

Shell Variables

Important shell variables include:

BASH
The path used to execute the current instance of Bash.
BASHOPTS
A colon-separated list of enabled options that were that were set using the shopt command. This can be useful for finding out if the shell environment will operate in the way that you want it to.
BASH_SOURCE
An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}.
BASH_SUBSHELL
The subshell level. This value is incremented by one within each subshell or subshell environment when the shell begins executing that environment. The initial value is 0.
BASH_VERSION
The version of Bash being executed, in human-readable form.
BASH_VERSINFO
A read-only array that holds version information for the current instance of Bash being executed. You can view its contents by running echo "${BASH_VERSINFO[@]}".
COLUMNS
The number of columns wide that are being used to draw output on the screen. Used by the select command.
DIRSTACK
The stack of directories that are available with the pushd and popd commands. Can also be displayed with the dirs command.
EDITOR
Name of the current user's preferred editor.
HISTCMD
The history number of the current command to be entered.
HISTCONTROL
If set to ignorespace, lines that start with a space are not added to the history file. If set to ignoredups, a line that duplicates the previous line is ignored, regardless of the times it is repeated.
HISTFILE
Defaults to ${HOME}/.bash_history and is set in the environment at log in.
HISTFILESIZE
Maximum number of lines stored in the command history file.
HISTIGNORE
Specifies which command lines can be unsaved.
HISTSIZE
Maximum number of commands allowed in command history memory.
HISTTIMEFORMAT
If set and not null, its value is used as a format string for strftime to print the time stamp associated with each history entry displayed by the history command. Time stamps are written to the history file so that they may be preserved across shell sessions.
HOME
The current user’s home directory.
HOSTNAME
The hostname of the computer at this time.
IFS
The internal field separator to separate input on the command line. By default, a space.
LANG
The current language and localization settings, including character encoding. Used to determine the locale category for any category not specifically selected with a LC_ variable.
LINES
The number of lines your display has. Used by the select command.
LS_COLORS
Defines color codes that are used to optionally add colored output to the ls command. Colors are used to distinguish different file types.
LOGNAME
Current user's username. Same as the USER variable.
MAIL
The path to the current user’s mailbox.
OLDPWD
The previous working directory as set by the cd command. This is kept by the shell in order to switch back to your previous directory by running cd -.
PAGER
Governs which program the man command (and others) use to display textual output.
PATH
List of directories containing executable programs that are eligible as external commands. When a user types in a command, the system will check directories in this order for the executable. PATH does not include the current directory (.), so if you want to run a program in the current directory, you can use the ./ syntax (this represents a null directory, which indicates the current directory at any given time), e.g., ./ex_program.
PIPESTATUS
An array of exit status values for the last foreground pipe (which may contain only a single command) that was executed. You can view its contents by running echo "${PIPESTATUS[@]}".
PS1
The primary command prompt definition (Prompt Statement 1). This is used to define what your prompt looks like when you start a shell session.
PS2
Used to declare secondary prompts for when a command spans multiple lines.
PS3
Used as the prompt for the select command. If not set, select prompts with a #.
PWD
The current working directory as set by the cd command.
RANDOM
Generates a random integer between 0 and 32767. Assigning a value to this variable seeds the random number generator.
SHELL
The full pathname to the shell. If not set at shell start, Bash assigns it the full pathname of the current user's login shell.
SHELLOPTS
A colon-separated list of enabled shell options that were set with the set command.
SHLVL
Shell level. A count of how deeply your Bash shell is nested. The count is incremented by one each time a new instance of Bash is started.
SUDO_EDITOR
Name of the current user's favorite sudo editor.
TERM
This specifies the type of terminal to emulate when running the shell. Different hardware terminals can be emulated for different operating requirements.
UID
The user ID of the current user.
USER
Current user's username; same as LOGNAME.

Shell Parameters

Like shell variables, shell parameters are entities that store values. There are two kinds of shell parameters:

  1. Positional Parameters These are the shell's command line arguments.
  2. Special Parameters These are parameters that are denoted by special characters. They may only be referenced, i.e., assignment is not allowed.

Positional Parameters

The positional parameters are denoted as $n, where n represents a number from 1. For numbers greater than 9, you must use curly braces when referring to the positional parameter (e.g., ${12}).

Special Parameters

The special parameters are as follows:

0
Resolves to the name of the shell or shell script. If the first character of this parameter's contents is a -, your shell is a login shell.
-
Resolves to the current options set for the shell. This is equivalent to what you see when run echo "${SHELLOPTS}".
#
Resolves to the number of positional parameters (arguments) that were passed on the command line.
_
At shell startup, resolves to the absolute pathname used to start the shell or script that is executed, as specified in the environment or argument list. After startup, resolves to the last argument of the previous foreground command (also set to the full pathname used to start each executed command and placed in the environment exported to that command). When checking mail, set to the name of the mail file currently being checked.
*
Resolves to all positional parameters, starting from 1. When variable substitution occurs within double quotes, it resolves to a single word, with the value of each parameter separated by the first character of the Internal Field Separator (IFS) variable. If the IFS is not set, the parameters are separated by spaces (e.g., {$1 $2 $3}).
@
Resolves to all positional parameters, starting from 1. When variable substitution occurs within double quotes, each parameter expands to a separate word (e.g., $1 $2 $3). This is an exception to the rule, i.e., word splitting never normally occurs within quotes.
$
Resolves to the process ID (PID) of the shell.
!
Resolves to the PID of the most recently executed background process.
?
Resolves to the exit status of the most recently executed foreground process. Can be used to check whether your command successfully completed or not (0 denotes success and 1 denotes failure).

Documentation

You can learn more about shell variables and parameters by referring to the bash man page and via GNU's Bash Reference Manual.

Enjoyed this post?

Subscribe to the feed for the latest updates.