Environment variables

Updated 4 Juillet 2019

Environment variables

What are environment variables?

An environment variable is a named object that contains information used by one or more applications. Some users, especially newcomers to Linux, tend to find this behaviour somewhat strange or unmanageable. But this is wrong as, in fact, you can easily tune software configurations with environment variables.

Important examples

The following table describes a number of variables used on Linux systems. Some examples are given below.

Variable Description
PATH This variable stores a list of colon-separated directories for the system to look for executable files. If you enter an executable name such as ls, rc-update or emerge, which is not in any of the directories listed here, it will not run (unless, of course, you have specified the full path, such as /bin/ls).
ROOTPATH This variable has the same function as PATH, but it lists only the directories you want to see when entering a command as root.
LDPATH This variable stores a list of colon-separated directories for the dynamic linker to look for libraries.
MANPATH This variable stores a list of colon-separated directories for the man command to look for man pages.
INFODIR This variable stores a list of colon-separated directories for the info command to look for info pages.
PAGER This variable stores the path to the utility that allows you to view the contents of files page by page, such as less or more.
EDITOR This variable stores the path to the file editor program, i.e. vi or nano.
KDEDIRS This variable stores a list of colon-separated directories containing KDE resources.
CLASSPATH This variable stores a list of colon-separated directories containing Java classes.
CONFIG_PROTECT This variable stores a list of space separated directories, protected by Portage from update.
CONFIG_PROTECT_MASK This variable stores a list of space-separated directories that are excluded from Portage protection at update time.

See below how all these variables can 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"

Globally defined variables

/etc/env.d

To define them in a more centralized way, Calculate uses the /etc/env.d directory. It contains a number of files, for example, "00basic", "05gcc" and so on, that define variables required by the package specified in the file name.

For instance, when you install gcc, its ebuild creates a file named /etc/env.d/05gcc that contains the following variable definitions:

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"

In other distributions, you are asked to modify or add definitions of environment variables in /etc/profile or elsewhere. On the other hand, Calculate makes it easy for you to support and manage your environment variables, freeing you from having to handle multiple files with variable definitions.

For instance, when gcc is updated, /etc/env.d/05gcc is also updated automatically.

This not only benefits Portage, but also the user. You may need to set a variable globally. Let's take http_proxy as an example. Instead of messing with /etc/profile, you can now simply create a ~~99local~ file and put all the necessary definitions to it:

/etc/env.d/99local

http_proxy="proxy.server.com:8080"

With the same file for all of your variables, you can easily see all of your variables together.

env-update

PATH is defined in multiple files in /etc/env.d. This is not an error: when you run env-update, definitions are merged before updating the environment variables, allowing packages (or users) to add their own values without interfering with existing ones.

The env-update script merges the values of the variables stored in the files located in /etc/env.d, in alphabetical order. The file names must begin with two decimal digits. This is how env-update operates:

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

Merging is performed for the following variables only: KDEDIRS, PATH, CLASSPATH, LDPATH, MANPATH, INFODIR, INFOPATH, ROOTPATH, CONFIG_PROTECT, CONFIG_PROTECT_MASK, PRELINK_PATH and PRELINK_PATH_MASK. For all the other variables, the value defined in the last file (among those stored in the alphabetical order in /etc/env.d) is used.

When you run the env-update script, all environment variables are created and put in /etc/profile.env (used by /etc/profile). Besides, the LDPATH+ value is used to create /etc/ld.so.conf. After that, ldconfig is run to update the /etc/ld.so.cache file used by the dynamic linker.

If you want the results of env-update operation to take effect immediately, run the following command to update the environment:

env-update && source /etc/profile

Note: This command updates variables only in the current terminal, new terminal instances and their descendants. It means that if you are working in an X11 environment, you will need to either type source /etc/profile in each new terminal, or restart X so that all new terminals address new variables. If you are using the Login Manager, log in as root and type /etc/init.d/xdm restart. If not, you will have to log out and log in again for X to generate descendants using the updated values.

Locally defined variables

User variables

All variables do not have to be defined globally. For example, a user may need to add ~/home/my_user/bin~~ and the current working directory (where they are now) to the PATH variable, but to their PATH variable only. To define an environment variable locally, edit ~/.bashrc or ~/.bash_profile. Example of PATH extension for local needs:

~/.bashrc

#(A colon without further directory entry means the current working directory)
PATH="${PATH}:/home/my_user/bin:"

Your PATH variable will be updated when you log out and then in again.

Session variables

Sometimes, even stronger restrictions are needed. You may need to be able to run executables in a dedicated temporary directory without specifying the full path to them and without having to edit ~/.bashrc.

In this case, you can simply define PATH for the current session with the export command. The variable will be assigned a temporary value until you exit the session. Example of defining an environment variable for a session:

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