Home » Linux Basics » 04 - Environment, Configuration, and Programming
4

Environmental Variables

Mappings that are used to store important configuration-related parameters

Environmental variables refer to value mappings that are used to store important configuration-related parameters. Most applications use parameters related to the application's context, user preferences, and etc. This sort of information may be stored in environmental variables. The values of environmental variables are relayed to an executing command, utility, or application by the executing process. The process itself acquires the values of environmental variables from the shell on which the command to start the program was issued. Environmental variable names are usually in all upper case. The Linux OS and associate utilities define several environmental variables for use in a number of contexts:

Variable Name Description Typical Value
CLASSPATH Colon-separated list of directories containing Java classes CLASSPATH="/usr/Java/jre/:.
EDITOR The path(s) to text editing utilities like vi and emacs. EDITOR="/usr/bin/vim:
/usr/bin/emacs/"
INFODIR Colon separated list of directories containing the man pages used by the info command INFODIR="/usr/share/info:
/usr/local/share/info"
LDPATH Location of libraries used to link and load computer programs LDPATH="/lib:/usr/lib:
/usr/local/lib:/usr/lib/gcc-lib/"
MANPATH Colon separated list of directories containing the man pages MANPATH="/usr/share/man:
/usr/local/share/man"
PATH A colon-separated list of directories searched for applications and executables when the corresponding command is issued. Running a command located in a directory that the path variable does not contain involves typing the entire path to the command. PATH="/bin:/usr/bin:/usr/local/bin:
/opt/bin:/usr/games/bin"
PS1 The prompt displayed to the user PS1="[\u] \W$ "
([user_name] current_directory$ )
ROOTPATH Directories searched when the superuser runs a command ROOTPATH="/sbin:/bin:/usr/sbin:
usr/bin:/usr/local/sbin:/usr/local/bin"

Issue the command 'set' or 'env' at the shell prompt to view a list of all currently set environmental variables. Users may define as many environmental variables as required. Special characters may be used to define environmental variables. Any environmental variable may be used as part of a command or to set execution parameters. The shell interprets any string preceded by a dollar symbol ($) as an environment variable.

    [ LinuxUser ] /usr/bin$ ls -l HOME
ls: cannot access HOME: No such file or directory
[ LinuxUser ] /usr/bin$ ls -l $HOME
total 14
-rw-r--r-- 1 LinuxUser None   57 Nov 24 21:21 errors
-rw-r--r-- 1 LinuxUser None   55 Nov 24 21:25 errors.txt
-rw-r--r-- 1 LinuxUser None 8728 Nov 24 20:19 lsinfo.txt
[ LinuxUser ] /usr/bin$

The above commands demonstrate how environmental variables may be used in commands. The ls HOME command returns an error message; there is no directory in /usr/bin that is called "HOME". However, when 'ls -l $HOME' is issued, the shell substitutes the appropriate value (the user's home directory) for $HOME; the contents of the user's home directory are displayed.

Of the list of environmental variables in the first table in the chapter, PATH is perhaps most integral. The PATH variable corresponds to the list of locations on the filesystem that contain all the commands a user is likely to use. When "mv" or "cat" or any other command is typed, the shell will search for the corresponding executable in all the directories in the PATH variable. The user may set any PATH related variable to temporarily correspond to an extra location:

    [ LinuxUser ] /usr/bin$ java HelloWorld
java.lang.NoClassDefFoundError: HelloWorld
Exception in thread "main"
[ LinuxUser ] /usr/bin$ export CLASSPATH=$CLASSPATH:$HOME/Java/build/classes
[ LinuxUser ] /usr/bin$ java HelloWorld
Hello World!!!!

The java class 'HelloWorld' simply prints out a "Hello World!!!!" message. The first time the user tries to run this program, an error message is displayed that points out that the class cannot be found. The user then appends the path to the HelloWorld class file onto the existing $CLASSPATH variable (export CLASSPATH=$CLASSPATH:$HOME/Java/build/). Afterwards, the Java Runtime Environment (this is the program that executes Java files) that uses the $CLASSPATH variable to find Java programs finds the HelloWorld program and returns the 'Hello World!!!!' message.

The PS1 environmental variable defines the shell command prompt that the user sees. The user may define the text, color and other format details of the font. Following are some of the backslash-escape special characters that are interpreted specially by the Bourne shell and may be used while defining command prompt environmental variables. The bourne shell interprets the value of these characters instead of treating them literally when they are encountered. There are many other backslash escape special characters, most of which have to do with setting the color and other visual properties of the prompt:

\aASCII bell character

\dCurrent date in weekday-month-day format(e.g. Tue Jun 10)

\hthe network hostname up to the first '.'

\Hthe hostname

\jthe number of current jobs tr>

\lthe shell's terminal device name

\nnewline

\rcarriage return

\tthe current time in 24-hour HH:MM:SS format

\Tthe current time in 12-hour HH:MM:SS format

\ucurrent user's username

\woffset from home directory

\Wthe current directory

\nnnthe symbol corresponding to the octal number nnn

\\a backslash

\e[0mcolor settings (0 = color code)

\[Used at the start of a sequence of escape characters

\]Used to end a sequence of escape characters

The above may be used as required in setting the four command prompts (PS1-4). The example below shows how the user may set her prompt to any arbitrary string. However, a meaningful prompt is always preferable. The prompt is reset to be of the form '[user_name] directory_path$ ' using the escape special characters corresponding to username (\u) and full path (\w).

    [ LinuxUser ] /usr/bin$ export PS1="Yes m'am? "
Yes m'am?

Yes m'am? export PS1="[\u] \w$ " [ LinuxUser ] /usr/bin$