RCS::Intro Linux and HPC::Editors


Page Contents:




About this document

Page Group







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.2
otherwise 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:

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'/g
since 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

  1. it reads the table of environment variables;
  2. defines a shell variable corresponding to each, with the same name;
  3. and copies the values across.
Thenceforth: Also:

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_VAR
or
  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