Skip to main content Skip to navigation

1.12 Environment variables

1. Introduction

Each process running under Linux has associated with it a collection of environment variables. These environment variables can be set or queried by the process, and this set of environment variables will be inherited by any subprocess that is spawned by this process.

Since just starting up a shell (in your case, an xterm) is a process, you will automatically have lots of environment variables already set for you. You can check what environment variables are set by typing env at your shell prompt. Try doing this now. There may be a lot of output. On each line, there will be an environment variables name (in capital letters), and its defintion.


Suppose the user phillipa is currently in the directory /home/phillipa/CASTEP/TIO2/ and types env at the prompt. A sample of the output is as follows;

2. The PATH environment variable

Most of these environment variables above should be pretty self-explanatory by now. One that deserves special attention is the PATH environment variable. In Linux, the programs you might like to use are kept in many different locations. It would be a pain to have to type out the path to, say, emacs (located in /usr/bin/), every time you wanted to use emacs. Instead, you can set the PATH environment variable, which stores a sequence of pathnames for your shell to search through. Each pathname in the sequence is separated by a colon. In the above example, for a given program, the shell will search /usr/local/bin first, then search /usr/bin next, then search /usr/X11R6/bin next, and so on. So, instead of typing /usr/bin/emacs to run emacs, phillipa just has to type emacs , and her PATH will locate the emacs program.

3. Setting/changing environment variables

Most of the environment variables in the example above are set automatically for you. However, you can change these envinronment variables, or set new ones. For example, you may wish to set your PATH environment variable to a special sequence of pathnames. In bash, you can set environment variable using the export command.


Suppose the user harry wants to run a program that needs to have a special environment variable set (this is a typical situation) called g03root. This environment variable needs to be set to the path /opt/g03. He needs to type at the shell prompt
export g03root=/opt/g03

4. Checking environment variables

Instead of finding out all environment variables, you can check individual environment variables, by using the echo command. In this case, the name of the environment variable must be prefixed with a $.


The user phillipa wishes to check her SHELL environment variable. She does so by typing:
echo $SHELL
The user harry wishes to check his g03root environment variable. He does so by typing:
echo $g03root