Any Linux shell has environment variables.  The command prompt is an example of a Linux shell.  The environment variables available to the command prompt can be used in commands and shell scripts.  Any programs run from the command prompt can also access the environment variables.  Programs such as make and perl do this.  The shell might have multiple environment variables.  Each variable consists of a name and a value.  The names must be unique and should use only accepted characters for identifiers (a-z, A-Z, 0-9, _).

The current set of environment variables can be printed by running the ‘env’ command.  A new environment variable can be created by running the ‘export’ command.  This creates a new environment variable in the current shell.  Each program or script that is run creates its own shell which contains a copy if its parent’s environment variables, so running a script that uses the export command will not affect the command prompt’s environment.  (Running a script with the ‘source’ command will allow it to do that.)  Environment variables can be removed using the ‘unset’ command.

Environment variables can make scripting easier, and they can make scripts more generally useful.  For example, a makefile might invoke the gcc compiler as $(CC).  This references an environment variable called ‘CC’ which must exist for the makefile to work.  A build machine can define CC in the environment before invoking make, and can therefore use any version of gcc which could be installed anywhere on the system without changing the makefile.  This is especially useful for cross-compiling.

Linux Environment Variables