3.4 Environment variables

Environment variables

What are they?

An environment variable is a named object that contains information used by one or more applications. Many users (and especially those new to Linux) find this a bit weird or unmanageable. However, this is a mistake: by using environment variables one can easily change a configuration setting for one or more applications.

Important examples

The following table lists a number of variables used by a Linux system and describes their use. Example values are presented in the table below.

Variable Description
PATH This variable contains a colon-separated list of directories in which your system looks for executable files. If you enter a name of an executable (such as ls, rc-update or emerge) but this executable is not located in a listed directory, your system will not execute it (unless you enter the full path as command, such as /bin/ls).
ROOTPATH This variable has the same function as PATH, but this one only lists the directories that should be checked when the root-user enters a command.
LDPATH This variable contains a colon-separated list of directories in which the dynamical linker searches through to find a library.
MANPATH This variable contains a colon-separated list of directories in which the man command searches for the man pages.
INFODIR This variable contains a colon-separated list of directories in which the info command searches for the info pages.
PAGER This variable contains the path to the program used to list the contents of files through (such as less or more).
EDITOR This variable contains the path to the program used to change the contents of files with (such as nano or vi).
KDEDIRS This variable contains a colon-separated list of directories which contain KDE-specific material.
CLASSPATH This variable contains a colon-separated list of directories which contain Java classes.
CONFIG_PROTECT This variable contains a space-delimited list of directories which should be protected by Portage during updates.
CONFIG_PROTECT_MASK This variable contains a space-delimited list of directories which should not be protected by Portage during updates.

Here's an example of how all these variables may be defined :

PATH="/bin:/usr/bin:/usr/local/bin:/opt/bin:/usr/games/bin" 
ROOTPATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin" 
LDPATH="/lib:/usr/lib:/usr/local/lib:/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3" 
MANPATH="/usr/share/man:/usr/local/share/man" 
INFODIR="/usr/share/info:/usr/local/share/info" 
PAGER="/usr/bin/less" 
EDITOR="/usr/bin/vim" 
KDEDIRS="/usr" 
CLASSPATH="/opt/blackdown-jre-1.4.1/lib/rt.jar:." 
CONFIG_PROTECT="/usr/X11R6/lib/X11/xkb /opt/tomcat/conf \
                /usr/kde/3.1/share/config /usr/share/texmf/tex/generic/config/ \
                /usr/share/texmf/tex/platex/config/ /usr/share/config" 
CONFIG_PROTECT_MASK="/etc/gconf" 

Defining variables globally

The /etc/env.d directory

To centralise the definitions of these variables, Calculate uses the /etc/env.d directory. Inside this directory you will find a number of files, such as 00basic, 05gcc, etc. which contain the variables needed by the application mentioned in their name.

For example, when you installed gcc, a /etc/env.d/05gcc file was created which contains the definitions of the following variables:

PATH="/usr/i686-pc-linux-gnu/gcc-bin/3.2" 
ROOTPATH="/usr/i686-pc-linux-gnu/gcc-bin/3.2" 
MANPATH="/usr/share/gcc-data/i686-pc-linux-gnu/3.2/man" 
INFOPATH="/usr/share/gcc-data/i686-pc-linux-gnu/3.2/info" 
CC="gcc" 
CXX="g++" 
LDPATH="/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3" 

Other distributions tell you to change or add such environment variable definitions in /etc/profile or other locations. Calculate on the other hand makes it easy for you to maintain and manage the environment variables without having to pay attention to the numerous files that can contain environment variables.

For instance, when gcc is updated, the /etc/env.d/05gcc file is updated too without requesting any user-interaction.

This not only benefits Portage, but also you, as user. Occasionally you might be asked to set a certain environment variable system-wide. As an example let us take the http_proxy variable. Instead of messing about with /etc/profile, you can now just create a file (/etc/env.d/99local) and enter your definition(s) in it:

http_proxy="proxy.server.com:8080"

By using the same file for all your variables, you have a quick overview on the variables you have defined yourself.

The env-update script

Several files in /etc/env.d define the PATH variable. This is not a mistake: when you run env-update, it will append the several definitions before it updates the environment variables, thereby making it easy for packages (or users) to add their own environment variable settings without interfering with the already existing values.

The env-update script will append the values in the alphabetical order of the /etc/env.d files. The file names must begin with two decimal digits. Update order used by env-update:

          00basic        99kde-env       99local
      +-------------+----------------+-------------+
PATH="/bin:/usr/bin:/usr/kde/3.2/bin:/usr/local/bin" 

The concatenation of variables does not always happen, only with the following variables: KDEDIRS, PATH, LDPATH, MANPATH, INFODIR, INFOPATH, ROOTPATH, CONFIG_PROTECT, CONFIG_PROTECT_MASK, PRELINK_PATH and PRELINK_PATH_MASK. For all other variables the latest defined value (in alphabetical order of the files in /etc/env.d) is used.

When you run env-update, the script creates all environment variables and places them in /etc/profile.env (which is used by /etc/profile). It will also extract the information from the LDPATH variable and use that to create /etc/ld.so.conf. After this, it will run ldconfig to recreate the /etc/ld.so.cache file used by the dynamical linker.

If you want to notice the effect of env-update immediately after you run it, execute the following command to update your environment:

env-update && source /etc/profile

Note: The above command only updates the variables in your current terminal, new consoles, and their children. Thus, if you are working in X11, you will need to either type source /etc/profile in every new terminal you open or restart X so that all new terminals source the new variables. If you use a login manager, become root and type /etc/init.d/xdm restart. If not, you will need to logout and log back in for X to spawn children with the new variable values.

Defining variables locally

User specific variables

You do not always want to define an environment variable globally. For instance, you might want to add /home/my_user/bin and the current working directory (the directory you are in) to the PATH variable but not want all other users on your system to have that in their PATH too. If you want to define an environment variable locally, you should use ~/.bashrc or ~/.bash_profile. Below is an example of extending PATH for local usage:

#(a colon followed by no directory is treated as the current working directory)
PATH="${PATH}:/home/my_user/bin:"

When you relogin, your PATH variable will be updated.

Session specific variables

Sometimes even stricter definitions are requested. You might want to be able to use binaries from a temporary directory you created without using the path to the binaries themselves or editing ~/.bashrc for the short time you need it.

In this case, you can just define the PATH variable in your current session by using the export command. As long as you do not log out, the variable will be using the temporary settings. Here is an example of defining a session specific variable:

export PATH="${PATH}:/home/my_user/tmp/usr/bin"
Thank you!