Beginners Guide to Using Shell Variables in Linux

Using Shell Variables

The Bash shell allows you to set shell variables that you can use to help run commands or to modify the behavior of the shell. You can also export shell variables as environment variables, which are automatically copied to programs run from that shell when they start. You can use variables to help make it easier to run a command with a long argument, or to apply a common setting to commands run from that shell.

Shell variables are unique to a particular shell session. If you have two terminal windows open, or two independent login sessions to the same remote server, you are running two shells. Each shell has its own set of values for its shell variables.

Assigning Values to Variables

Assign a value to a shell variable using the following syntax:


Variable names can contain uppercase or lowercase letters, digits, and the underscore character (_). For example, the following commands set shell variables:

[user@host ~]$ COUNT=40
[user@host ~]$ first_name=John
[user@host ~]$ file1=/tmp/abc
[user@host ~]$ _ID=GEEK

Remember, this change only affects the shell in which you run the command, not any other shells you may be running on that server. You can use the set command to list all shell variables that are currently set. (It also lists all shell functions, which you can ignore.) This list is long enough that you may want to pipe the output into the less command so that you can view it one page at a time.

[user@host ~]$ set | less
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote: force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath
...output omitted...

Retrieving Values with Variable Expansion

You can use variable expansion to refer to the value of a variable that you have set. To do this, precede the name of the variable with a dollar sign ($). In the following example, the echo command prints out the rest of the command line entered, but after variable expansion is performed. For example, the following command sets the variable COUNT to 40.

[user@host ~]$ COUNT=40

If you enter the command echo COUNT, it will print out the string COUNT.

[user@host ~]$ echo COUNT

But if you enter the command echo $COUNT, it will print out the value of the variable COUNT.

[user@host ~]$ echo $COUNT

A more practical example might be to use a variable to refer to a long file name for multiple commands.

[user@host ~]$ file1=/tmp/tmp.z9pXW0HqcC
[user@host ~]$ ls -l $file1 -rw-------. 1 geek geek 1452 Jan 22 14:39 /tmp/tmp.z9pXW0HqcC
[user@host ~]$ rm $file1
[user@host ~]$ ls -l $file1
total 0
[user@host ~]$ echo Repeat $COUNTx
[user@host ~]$ echo Repeat ${COUNT}x
Repeat 40x

Configuring Bash with Shell Variables

Some shell variables are set when Bash starts but can be modified to adjust the shell’s behavior. For example, two shell variables that affect the shell history and the history command are HISTFILE and HISTFILESIZE. If HISTFILE is set, it specifies the location of a file to save the shell history in when it exits. By default this is the user’s ~/.bash_history file. The HISTFILESIZE variable specifies how many commands should be saved in that file from the history.

Another example is PS1, which is a shell variable that controls the appearance of the shell prompt. If you change this value, it will change the appearance of your shell prompt. A number of special character expansions supported by the prompt are listed in the “PROMPTING” section of the bash(1) man page.

[user@host ~]$ PS1="bash\$ "
bash$ PS1="[\u@\h \W]\$ "
[user@host ~]$

Two items to note about the above example: first, because the value set by PS1 is a prompt, it is virtually always desirable to end the prompt with a trailing space. Second, whenever the value of a variable contains some form of space, including a space, a tab, or a return, the value must be surrounded by quotes, either single or double; this is not optional. Unexpected results will occur if the quotes are omitted. Examine the PS1 example above and note that it conforms to both the recommendation (trailing space) and the rule (quotes).

Configuring Programs with Environment Variables

The shell provides an environment to the programs you run from that shell. Among other things, this environment includes information on the current working directory on the file system, the command-line options passed to the program, and the values of environment variables. The programs may use these environment variables to change their behavior or their default settings. Shell variables that are not environment variables can only be used by the shell. Environment variables can be used by the shell and by programs run from that shell

You can make any variable defined in the shell into an environment variable by marking it for export with the export command.

[user@host ~]$ EDITOR=vim
[user@host ~]$ export EDITOR

You can set and export a variable in one step:

[user@host ~]$ export EDITOR=vim

Applications and sessions use these variables to determine their behavior. For example, the shell automatically sets the HOME variable to the file name of the user’s home directory when it starts. This can be used to help programs determine where to save files.

Another example is LANG, which sets the locale. This adjusts the preferred language for program output; the character set; the formatting of dates, numbers, and currency; and the sort order for programs. If it is set to en_US.UTF-8, the locale will use US English with UTF-8 Unicode character encoding. If it is set to something else, for example fr_FR.UTF-8, it will use French UTF-8 Unicode encoding.

[user@host ~]$ date
Tue Jan 22 16:37:45 CST 2019
[user@host ~]$ export LANG=fr_FR.UTF-8
[user@host ~]$ date
mar. janv. 22 16:38:14 CST 2019

Another important environment variable is PATH. The PATH variable contains a list of colonseparated directories that contain programs:

[user@host ~]$ echo $PATH
/home/user/.local/bin:/home/user/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/ bin:/usr/local/sbin:/usr/sbin

When you run a command such as ls, the shell looks for the executable file ls in each of those directories in order, and runs the first matching file it finds. (On a typical system, this is /usr/ bin/ls.)

You can easily add additional directories to the end of your PATH. For example, perhaps you have executable programs or scripts that you want to run like regular commands in /home/user/sbin. You can add /home/user/sbin to the end of your PATH for the current session like this:

[user@host ~]$ export PATH=${PATH}:/home/user/sbin

To list all the environment variables for a particular shell, run the env command:

[user@host ~]$ env
...output omitted...
...output omitted...

Setting the Default Text Editor

The EDITOR environment variable specifies the program you want to use as your default text editor for command-line programs. Many programs use vi or vim if it is not specified, but you can override this preference if required:

[user@host ~]$ export EDITOR=nano

Setting Variables Automatically

If you want to set shell or environment variables automatically when your shell starts, you can edit the Bash startup scripts. When Bash starts, several text files containing shell commands are run which initialize the shell environment. The exact scripts that run depend on how the shell was started, whether it is an interactive login shell, an interactive non-login shell, or a shell script.

Assuming the default /etc/profile, /etc/bashrc, and ~/.bash_profile files, if you want to make a change to your user account that affects all your interactive shell prompts at startup, edit your ~/.bashrc file. For example, you could set that account’s default editor to nano by editing the file to read:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc

# User specific environment
export PATH

# User specific aliases and functions
export EDITOR=nano

Unsetting and Unexporting Variables

To unset and unexport a variable entirely, use the unset command:

[user@host ~]$ echo $file1
[user@host ~]$ unset file1
[user@host ~]$ echo $file1

[user@host ~]$

To unexport a variable without unsetting it, use the export -n command:

[user@host ~]$ export -n PS1