Shell Variables and Environment Variables
1. |
Environments of Processes and Environment Variables |
Every process runs within its own environment which consists of a set of variable=value text-pairs. A process can access the values of these environment variables much like it can access its given (command-line) arguments.
A process environment is inherited by child processes. In particular, any process started from the command-line shell inherits the shell's environment at that time.
Examples
The syntax in all examples in this section assume the BASH shell is being used.
When a command is given to a shell, such as BASH, whether at the commandline or within a script, the shell looks in a sequence of directories — the search path — for the excutables corresponding to the command. This search path is determined by the PATH environment variable. Examples:
prompt> export PATH=/usr/local/bin:/usr/bin:/bin prompt> export PATH=$PATH:/usr/local/sge/bin/lx24-amd64
Some application software requires given environment variables to be set in order to work properly. For example Sun Grid Engine requires SGE_ROOT to be set to the top-level directory of the SGE installation, such as
export SGE_ROOT=/usr/local/sge_v6.2otherwise we receive such pleasantries as
Unable to initialize environment because of error: Please set the environment variable SGE_ROOT. Exiting.
Many commercial software packages, including Fluent and Star-CD, use the FlexLM licence-management system. Such applications will refuse to run unless they "checkout" a licence; the location of a licence is found by the application from the value of the environment variable LD_LICENSE_FILE, for example:
LM_LICENSE_FILE="[email protected]"N.B. The american spelling of licence is required here.
When a dynamically-linked binary is executed, the OS looks for required shared-object libraries (.so files) in a sequence of directories including /lib and /usr/lib, and also those specified in LD_LIBRARY_PATH. Examples:
prompt> export LD_LIBRARY_PATH=SGE_ROOT/lib/lx24-amd64/ prompt> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/openmpi-1.3-gcc-ifort-10.1.012/lib
2. |
Shell Variables |
Shells, such as tcsh and bash, maintain a set of internal variables known as shell variables. Shell variables:
- cause the shell to work in a particular way;
- are local to shell in which they are defined — are not available to (parent or) child processes;
- are traditionally given in lower case for the C shell family and upper case for the Bourne shell family.
Examples
The prompt for BASH, TCSH, etc., is controlled by a shell variable. In the case of BASH, this is PS1 — commonly this is set to something like
PS1='\u@\h:\w\$ 'which gives username (\u), hostname (\h) and path of working directory (\w), viz
rcs@mctalby:~/public_html$
3. |
The /proc Directory |
Each process running on a Linux system has a representation in the pseudo-filesystem /proc, in /proc/<pid> — the PID (process ID) can be determined from the output of ls -l. To view a process' environment
cat /proc/<pid>/environ | tr '\0' '\n'or
cat /proc/<pid>/environ | sed s/'\x0'/'\n'/gsince environ is a null-separated list.
4. |
Bourne Shell Family Including BASH |
Shell and environment variables are handled by the Bourne family differently from the C Shell family. When a Bourne family shell starts
- it reads the table of environment variables;
- defines a shell variable corresponding to each, with the same name;
- and copies the values across.
- the shell refers only to its internal shell variables;
- for a change made to a shell variable to be reflected in the environment — and therefore inherited by child processes — it must be explicitly exported to the corresponding environment variable.
- there exist additional shell variables, so that after starting, the environment is a strict subset of the shell's variables.
4.1. |
Setting, Exporting and Deleting Variables |
To change or set a shell variable:
bash> MY_VAR=value bash> MY_LONG_VAR="long value"
To change or set a shell variable and export it:
bash> A_VAR=value bash> export A_VARor
bash> export A_VAR=value
To delete a variable from both the set of shell variables and he environment:
unset THE_VAR
Example
bash> echo $PATH /usr/local/bin:/usr/bin:/bin bash> export PATH=$PATH:/home/simonh/bin bash> echo $PATH /usr/local/bin:/usr/bin:/bin:/home/simonh/bin
4.2. |
Listing Shell and Environment Variables |
To see the environment use the env command with no arguments, perhaps piping the output into sort:
bash> env | sort DISPLAY=localhost:10.0 HOME=/home/rcs . . . . PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games PWD=/home/rcs . . . . TERM=xterm USER=rcs _=/usr/bin/env
To see the shell's own variables:
bash> set BASH=/bin/bash BASH_ARGC=() . . . . DISPLAY=localhost:10.0 EUID=1007 . . . . PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games PIPESTATUS=([0]="0") . . . . TERM=xterm UID=1007 USER=rcs _=set
5. |
C Shell Family (incl. tcsh) |
5.1. |
Shell Variables |
Shell variables are defined
csh> set <name>=<value> # ...note the "=" sign...and deleted
csh> unset <name>
To use a shell variable
csh> $<name>or
csh> ${<name>} # ...eliminates ambiguity if catenated with text...
To see the value of a shell variable
csh> echo $<name>
Example
csh> set history=1000 # ...increase size of history...
5.2. |
Environment Variables |
Environment variables are defined
csh> setenv <name> <value> # ...no "=" sign...and deleted
csh> unsetenv <name>
Example
csh> echo $PATH /usr/local/bin:/usr/bin:/bin csh> setenv PATH ${PATH}:/home/simonh/bin # ...Note the braces... csh> echo $PATH /usr/local/bin:/usr/bin:/bin:/home/simonh/bin
5.3. |
Listing Shell and Environment Variables |
To see the environment use the command setenv with no argument, and perhaps pipe the output into sort:
csh> setenv | sort DISPLAY=localhost:12.0 HOME=/home/rcs LANGUAGE=en_GB:en_US:en_GB:en LOGNAME=rcs MAIL=/var/mail/rcs OLDPWD=/home/rcs PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games PWD=/home/rcs/public_html SHELL=/bin/bash SHLVL=1 SSH_CLIENT=130.88.1.146 58382 22 SSH_CONNECTION=130.88.1.146 58382 130.88.1.144 22 SSH_TTY=/dev/pts/2 TERM=xterm USER=rcs _=/bin/csh
To see the shell's own variables, use the set command, with no arguments:
csh> set argv () cwd /home/rcs/public_html home /home/rcs path (/usr/local/bin /usr/bin /bin /usr/bin/X11 /usr/games) prompt % prompt2 ? shell /bin/csh status 0 term xterm user rcs