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:

The path used to execute the current instance of Bash.
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.
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]}.
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.
The version of Bash being executed, in human-readable form.
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[@]}".
The number of columns wide that are being used to draw output on the screen. Used by the select command.
The stack of directories that are available with the pushd and popd commands. Can also be displayed with the dirs command.
Name of the current user's preferred editor.
The history number of the current command to be entered.
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.
Defaults to ${HOME}/.bash_history and is set in the environment at log in.
Maximum number of lines stored in the command history file.
Specifies which command lines can be unsaved.
Maximum number of commands allowed in command history memory.
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.
The current user’s home directory.
The hostname of the computer at this time.
The internal field separator to separate input on the command line. By default, a space.
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.
The number of lines your display has. Used by the select command.
Defines color codes that are used to optionally add colored output to the ls command. Colors are used to distinguish different file types.
Current user's username. Same as the USER variable.
The path to the current user’s mailbox.
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 -.
Governs which program the man command (and others) use to display textual output.
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.
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[@]}".
The primary command prompt definition (Prompt Statement 1). This is used to define what your prompt looks like when you start a shell session.
Used to declare secondary prompts for when a command spans multiple lines.
Used as the prompt for the select command. If not set, select prompts with a #.
The current working directory as set by the cd command.
Generates a random integer between 0 and 32767. Assigning a value to this variable seeds the random number generator.
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.
A colon-separated list of enabled shell options that were set with the set command.
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.
Name of the current user's favorite sudo editor.
This specifies the type of terminal to emulate when running the shell. Different hardware terminals can be emulated for different operating requirements.
The user ID of the current 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:

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


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.