The environment of a process consists of a set of environment variables. Each of these variables has a name and can store text. There are environment variables telling the system which screen the user is using, where his home directory is, what his current directory is, what type of system is being used, what the prompt looks like and what the search path for programs is. In addition to these standard variables programs can define and use their own variables. Every process inherits the environment from its calling process, which in most cases is the shell. The shell however does not take on changes made by its children to the environment, in the same way children get their looks from their parents, but not vice versa. The shell can be used to create new environment variables, to change existing variables or delete old ones. Every shell has different commands to do this.
The shell we are using is bash. Bash has two commands to manipulate environment variables. The unset command removes variables from the environment. The export command adds or manipulates environment variables. To list all current environment variables we can simply type export:
$export declare -x DISPLAY="nietzsche:0.0" declare -x HOME="/home/local/markst" declare -x HOSTNAME="nietzsche.cs.rhbnc.ac.uk" declare -x HOSTTYPE="i386" declare -x LOGNAME="markst" declare -x OSTYPE="Linux" declare -x PATH=":/bin:/usr/bin:/usr/X11R6/bin:/usr/X11/bin:." declare -x PS1="\\ h:\`pwd\` > " declare -x PS2=">> " declare -x SHELL="/bin/bash" declare -x TERM="xterm" declare -x USER="markst" $
We can see what display we are using, what our home directory is, what the name of the computer is we are using, the type of computer, our login name, the operating system's name and the path, which is the directories the shell scans when looking for a program. PS1 is the primary prompt, PS2 is the secondary prompt, then we see what the shell is we are using, what type of terminal we are using and what our user name is. This is just an extract from the environment, giving a few important variables.
If we just want to check the contents of a variable we can use the shell's echo command. This command takes text and prints it out. It can also print the contents of a variable if the variable's name is prefixed with a $:
$echo Hello Hello $echo The user variable contains: $USER The user variable contains: markst $
Say we want to delete the USER environment variable:
$unset USER $echo The user variable contains: $USER The user variable contains: $
To check if the variable has been deleted we can use the export command on its own without a variable name.
Say we now want to add the USER variable again:
$export USER=markst $echo The user variable contains: $USER The user variable contains: markst $
If we want to create a new variable, which has a space as part of its contents we have to put it in double quotes:
$export USER2="Nick Dunkin" $echo $USER2 Nick Dunkin $
We can also create environment variable, which take their information from other environment variables:
$export USER2="$USER2 and $USER" $echo $USER2 Nick Dunkin and markst $