Bash Environment Variables

Part of the book: Bash: The Linux Command Line

The shell is configured in a few different ways, but one of the main ways is through what are called environment variables. These variables are not only used by the shell, but can be accessed by all Linux programs.

To explain environment variables I'll start by showing one of the most basic variables, the PATH variable. This variable defines which directories are searched to find commands. So when you type a command like vi the Bash shell looks at the contents of the PATH variable and searches each of the directories for a file called vi. The first one that is found is executed. So let's look at the contents of PATH by typing this into a shell prompt:

echo $PATH

You might see something like this:

/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

We used the echo command to print something to the screen and if we put a dollar sign in front of an environment variable name the shell actually substitutes the contents of the variable before running the command line.

The above is my PATH variable and it shows many directories are separated with colons.

Another way to look at environment variables is the set command. Run set and you see a huge list of variables that exist.

To set or change a variable you can use the following syntax:

VARIABLE="value"

Commonly we might add something to PATH. For example you may create scripts in a bin directory in your home directory and want to run these without putting a full path name in for each command. This is a special example because we want to add to the value not replace it. To do this we can use the PATH variable while setting it. So to add /home/john/bin to the PATH we would run:

PATH="$PATH:/home/john/bin"

In the above example, Bash substitutes $PATH for the contents of the PATH variable before assigning the new value to PATH. This way the new directory is added to the end.

Environment variables also have a certain property. We often want processes that we run from a shell to inherit the environment variables that we set. If we simply define variables like above it only affects the variable for this shell and not child programs. To allow subsequent programs to inherit the variable we need to export the variable:

export PATH

Shell Prompt

One other common use for environment variables is to redefine what the shell prompt looks like. This is held in the PS1 and PS2 variables. Setting PS1='$ ' sets the command prompt to a very simple dollar sign. If you want to get elaborate there are many variables that you can put into the command prompt so that it displays things like the host name, current directory, date and other variable facts. Let's change this to the host name and current directory. To do this we will use the \h and \w variables. In order for the backslash characters to be left alone we need to make sure that we use single quotes, not double quotes when we set the variable.

PS1='\h@[\w]$ '
export PS1

To find other variables to use in prompts view the man page for bash (i.e. run man bash) and search for the PROMPTING section.

Clearing variables

One may think that by setting a variable to an empty string will clear it. In fact it does not. There is a different between an unset variable and variable that is set to an empty string.

To delete a variable use the unset command:

unset PS1

This will make your prompt nothing. To restore it repeat the PS1 setting in the Shell Prompt section above.

Making permanent changes

You will find that any changes that you make in the shell only work for that shell instance and any shell or program launched from it. As soon as you exit the shell the environment is lost.

All these changes disappear when you exit the shell. If you want to configure a variable that is permanent edit the .bashrc file in your home directory and add the above lines to the end of the file. You may also find environment variables set in your .profile file as well, although changes to this file will require logging out of the GUI and back in to see the changes.

System-Wide Changes

There are other places where environment variables are set. You can set them for all users in the /etc/profile and /etc/bashrc files.

How Other Programs Use Environment Variables

Programs other than Bash can be configured using environment variables too. One that comes to mind is rsync. This program synchronises files between two directories even if they are on different servers. It uses a few different variables but the one I use most is RSYNC_RSH which tells rsync to use ssh to connect to remote servers.

The easist way to see if a program uses environment variables is to look at its man page. Search the page for the word ENV.

Other ways to set Variables

When executing another program you can specify environment variables before the name of the command. So to set rsync to use ssh we need to set the RSYNC_RSH variable. We could set as we have done above and export it, or we can specify it on the command line before the command:

RSYNC_RSH=ssh rsync /source/dir /dest/dir

The variable is passed to the command but is never set in the current shell. We can demonstrate this by running bash and giving an echo command as an argument:

H=Hello W=World bash -c 'echo sub shell: $H $W'
echo 'this shell: $H $W'

We see the words "sub shell: Hello World" printed to the screen. If we were to repeat the same echo command on the next line by itself it would print only "this shell:" because the variables were never set in the current shell.

NOTE: The use of single quotes is important. Any dollar signs used within single quotes are not interpreted as variable names. This way the bash sub shell actually sees the dollar signs. If we used double quotes the current shell would replace the variables before the sub shell was executed.