RCS::Intro Linux and HPC::Editors


Page Contents:


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. However, 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.

2. 

Examples

  PATH, LD_LIBRARY_PATH, SGE_ROOT

3. 

Shell Variables

Shells, such as tcsh and bash, maintain a set of internal variables known as shell variables. Shell variables:

4. 

C Shell Family (incl. tcsh)

4.1. 

Shell Variables

Shell variables are defined

  set <name>=<value>
      # ...note the "=" sign...
and deleted
  unset <name>

To use a shell variable

  $<name>
or
  ${<name>}
      # ...eliminates ambiguity if catenated with text...

To see the value of a shell variable

  echo $<name>

Example

  set history=1000
      # ...increase size of history...

4.2. 

Environment Variables

Environment variables are defined

  setenv <name> <value>
      # ...no "=" sign...
and deleted
  unsetenv <name>

Example

  tcsh> echo $PATH
  /usr/local/bin:/usr/bin:/bin

  tcsh> setenv PATH ${PATH}:/home/simonh/bin
      # ...Note the braces...

  tcsh> echo $PATH
  /usr/local/bin:/usr/bin:/bin:/home/simonh/bin

4.3. 

Listing Shell and Environment Variables


5. 

Bourne Shell Family (incl. 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:

5.1. 

Setting, Exporting and Deleting Variables

To change or set a shell variable:

  MY_VAR=value
  MY_LONG_VAR="long value"

To change or set a shell variable and export it:

  A_VAR=value
  export A_VAR
or
  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

5.2. 

Listing Shell and Environment Variables

To see the environment:

  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 shells 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

6. 

/proc

Each process running on a Linux system has a representation in the pseudo-filesystem /proc, in /proc/<pid>. 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.