Environment Variables

Introduction

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.

They are part of the operating environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

They were introduced in their modern form in 1979 with Version 7 Unix, so are included in all Unix operating system flavors and variants from that point onward including Linux and OSX. From PC-DOS 2.0 in 1982, all succeeding Microsoft operating systems including Microsoft Windows, and OS/2 also have included them as a feature, although with somewhat different syntax, usage and standard variable names.

Environment variables differ from shell variables. Environment variables are inherited by child programs and sessions. Shell variables are strictly internal to the running shell.

Usage

Which Shell Am I Running?

First, you need to determine which shell you're running. The syntax of environment variable-management commands varies by shell, but fortunately the underlying concepts are the same. You can print the name of your currently-running shell or default shell in a variety of ways:

Currently-Running Shell

  1. echo $0 prints the name of the currently-running program. When executed from a command prompt, it displays the name of the currently-running shell:
    
    [timberlake] ~% echo $0
    -tcsh
    [timberlake] ~% bash
    bash-3.2$ echo $0
    bash
    bash-3.2$ csh
    [timberlake] ~% echo $0
    csh
    [timberlake] ~% 
    
    

Default Shell

  1. The SHELL environment variable displays your default or initial shell:
    
    % printenv SHELL
    /bin/tcsh
    
    
  2. The env command shows the values of your default or initial shell's environment variables. Use the grep pattern-matching utility to clip out only the SHELL environment variable.
    
    % env | grep SHELL
    SHELL=/bin/tcsh
    
    
  3. The /etc/passwd file shows all of the host system's account records. Each account record's last field shows the user's default or initial shell. The system starts this shell for the user when the user logs in, opens a new window, or starts a new session. I used the grep pattern-matching utility to match a keyword (my userid) to display a single record.
    
    % grep cwmiller /etc/passwd
    cwmiller:x:24368:5564:Christian Miller:/home/csestaff/cwmiller:/bin/tcsh
                                                                   ^^^^^^^^^
    

Operations

Shell Print Set Prepend Append Unset
tcsh % printenv VARIABLENAME % setenv VARIABLENAME [VALUES] % setenv VARIABLENAME NEWVALUE:${VARIABLENAME} % setenv VARIABLENAME ${VARIABLENAME}:NEWVALUE % unsetenv VARIABLENAME
bash % printenv VARIABLENAME % export VARIABLENAME=[VALUES] % export VARIABLENAME=NEWVALUE:${VARIABLENAME} % export VARIABLENAME=${VARIABLENAME}:NEWVALUE % unset VARIABLENAME

Examples

In these examples, the command tcsh starts up a fresh shell as a command. It is used for demonstration purposes only (it is not usually something you want to do).

Example 1: Shell variables are not passed on to child processes


% set prompt = "\! > "
20 % set message = "Hello World"
21 % echo $message
Hello World
22 % tcsh
% echo $message
message: Undefined variable.
%

Example 2: Environment variables are passed on to child processes


23 % echo $MESSAGE
MESSAGE: Undefined variable.
24 % setenv MESSAGE "Hi Mom."
25 % echo $MESSAGE
Hi Mom.
26 % tcsh
% echo $MESSAGE
Hi Mom.
%

Notes

  1. Environment variables can be used to pass information to other utility programs.
  2. Each UNIX command should have a section in its manual page called ENVIRONMENT. This section describes any environment variables that that command looks for and how they effect the command.
  3. The environment variable MANPATH tells the man command where it should look for manual pages.
  4. The default is directory /usr/share/man on Solaris but your sys-admin may want to add others (e.g. /usr/local/man).
  5. Usually paths in environment variables need to be directory names separated by colons (a kickback to Bourne Shell syntax):
    
    % setenv MANPATH /usr/share/man:/usr/local/man
    
    
  6. The shell treats some shell variables (such as term) specially - if you set term, the shell will automatically set the matching environment variable (TERM) for you.
  7. If your sys-admin sets something like MANPATH in the system startup file, you can extend it in your ~/.cshrc:
    
    setenv MANPATH ${MANPATH}:~/man
    
    

References

  1. http://en.wikipedia.org/wiki/Environment_variable