Environment Variables

What are environment variables?

In Unix based systems, environment variables are variables that are defined for the current shell and are inherited by any child shells or processes. Environmental variables contain information that tells your shell where to find resources.

Why should I care about environment variables?

Environment variables allow you to customize your Unix environment. For example, you may change the default text editor, the path to executable files and libraries by setting appropriate environment variables. On the CS machines, additional software can be used by tweaking your environment variables.

How do environment variables look like?

The environment variables look like strings that represent key-value pairs. They generally look something like this:

KEY=value
KEY="Some other value"
KEY=value1:value2

Note:

  • The names of the variables are case-sensitive. By convention, environment variables should have UPPER CASE names.
  • If multiple values are passed for a key, they should be separated by colon (:) characters.
  • If the value contains spaces, quotes should be used
  • There is no space around the equals = symbol.

What are some important environment variables that I should know?

Here are some of the important variables:

Variable Name Description
HOME The current user’s home directory.
LD_LIBRARY_PATH An ordered list of directories that are checked when looking for libraries.
PATH An ordered list of directories that are checked when looking for binaries (commands).
PYTHONPATH An ordered list of directories that are checked when looking for Python libraries (modules).
SHELL The default shell for you. It is set to be bash by default, but other values can be set if you prefer other options.
How do I set environment variables?

You can use the export command to define and set environment variables. For example, if you want to set a new variable named FOO, having the value bar, use the following command:

export FOO="bar"

How do I confirm that the environment variable configuration has been applied?

You can confirm by using the printenv command. To see if the FOO variale we defined in the previous question was successfully set, use:

printenv $FOO
bar

You should see the value you had defined for the variable.

Environment variables created in a terminal shell are available only in the current session. If you open a new shell or if you log out all variables will be lost. For making them persistent, see the bashrc guide


How do I delete environment variables?

You can use the unset command to remove environment variables. For example, to delete the variable we created earlier:

unset FOO

Is export PATH=/usr/local/foo/latest/bin:$PATH the same as export PATH=$PATH:/usr/local/foo/latest/bin ?

No. The order matters. When a user types in a command, the system checks directories in this order for the executable. So, for export PATH=/usr/local/foo/latest/bin:$PATH, the location /usr/local/foo/latest/bin is searched before any other location in the binaries path. And for export PATH=$PATH:/usr/local/foo/latest/bin the /usr/local/foo/latest/bin is searched for at the last.

In fact, by changing the order of directory locations in your environment variables, you can tweak which locations get more preference for locating software. This is useful if a software is available from more than one location.