Each process on linux has an associated list of name value pairs called the environment. A process can manipulate its environment and the environment of its children, but not vice versa. In a shell process one can interactively control the environment, including controlling what variables are provided (exported) to child processes.

setting local variables

To set a variable in the shell's environment only, you do name=value in bash, and set name=value in tcsh.

setting variables seen by children

To create an exported variable, you do export name=value in bash, and setenv name value in tcsh respectively.

setting just for children

Another thing to note is that bash allows you to set a variable just for the subprocess with name=value program. For e.g. LANG=C sort will set the LANG=C variable for the sort program, while leaving the shell's LANG variable alone. The same thing can be achieved in tcsh with the env program. For e.g. env LANG=C sort

reading variables

To refer to a particular name one uses $name, so you can do echo $name for e.g. In bash, you can see all variables using the set command, and all exported variables using printenv program. For tcsh set shows variables that are not exported, and printenv is the same as for bash except that it's a shell builtin.

Environment uses

An environment variable can be used for any purpose by a process. In the example above, "sort" will sort in ASCII order when LANG=C. In the shell, environment variables are commonly used while programming, for standard variable storage. Also in the shell, special variable names can be used to communicate to and from the shell. An example of communication to the shell is, setting the PATH variable will tell the shell in which directories to look for commands. An example of communication from the shell is, the shell always sets the PWD variable appropriately as the current working directory is changed. Considering that children inherit the exported environment of their parent, and most processes can trace their ancestry to a shell, the shell startup files are a very common/handy place to set environment variables. To get details on these startup files, for bash at least, do: info bash "Bash Features" "Bash Startup Files".
© Apr 3 2006