Texas A&M Supercomputing Facility Texas A&M University Texas A&M Supercomputing Facility

Shell Variables and Environment Variables

Every UNIX process runs in a specific environment. An environment consists of a table of environment variables, each with an assigned value. When you log in certain login files are executed. They initialize the table holding the environment variables for the process. When this file passes the process to the shell, the table becomes accessible to the shell. When a (parent) process starts up a child process, the child process is given a copy of the parent process' table. Environment variable names are generally given in upper case by convention.

The shell also maintains a set of internal variables known as shell variables. These variables cause the shell to work in a particular way. Shell variables are local to the shell in which they are defined; they are not available to the parent or child shells. By convention, shell variable names are generally given in lower case in the C shell family and upper case in the Bourne shell family.

C Shell Family

The C shell family explicitly distinguishes between shell variables and environment variables.

Shell Variables

A shell variable is defined by the set command and deleted by the unset command. The main purpose of your .cshrc file (discussed later in this chapter) is to define such variables for each process. To define a new variable or change the value of one that is already defined, enter:

% set name=value

where name is the variable name, and value is a character string that is the value of the variable. If value is a list of text strings, use parentheses around the list when defining the variable, e.g.,

% set name=(value1 value2 value3)

The set command issued without arguments will display all your shell variables. You cannot check the value of a particular variable by using set name, omitting =value in the command; this will effectively assign an empty value to the variable.

To delete, or unset, a shell variable, enter:

% unset name

To use a shell variable in a command, preface it with a dollar sign ($), for example $name. This tells the command interpreter that you want the variable's value, not its name, to be used. You can also use ${name}, which avoids confusion when concatenated with text.

To see the value of a single variable, use the echo command:

% echo $name

If the value is a list, to see the value of the nth string in the list enter:

% echo $name[n]

The square brackets are required, and there is no space between the name and the opening bracket.

To prepend or append a value to an existing shell variable, use the following syntax:

% set name=prepend_value${name}

or

% set name=${name}append_value

Note that when a shell is started up, four important shell variables are automatically initialized to contain the same values as the corresponding environment variables. These are user, term, home and path. If any of these are changed, the corresponding environment variables will also be changed.

Environment Variables

Environment variables are set by the setenv command, and displayed by the printenv or env commands, or by the echo command as individual shell variables are. Some environment variables are set by default (e.g., HOME, PATH).

The formats of the commands are (note the difference between set and setenv):

% setenv [NAME value]
% unsetenv NAME 

where value is interpreted as a character string. If the string includes blanks (i.e. if it encompasses multiple values), enclose the string in double quotes ("), e.g.,

% setenv NAME "value1 value2 ..."

The current environment variable settings can be displayed using the setenv command with no arguments.

To use an environment variable in a command, preface it with a dollar sign ($), for example $NAME. This tells the command interpreter that you want the variable's value, not its name, to be used. You can also use ${NAME}, which avoids confusion when concatenated with text.

To prepend or append a value to an existing environment variable, use the following syntax:

% setenv NAME "prepend_value${NAME}"

or

% setenv NAME "${NAME}append_value"

If the pre- or appended value is the value of a preexisting environment variable, enclose the variable name in braces, too, e.g.,

% setenv NAME "${NAME}${XYZ_VAR}"

Appending and prepending is commonly used with the PATH variable, and a colon is used as a separator, e.g.,

% setenv PATH "${PATH}:${XYZ_DIR}"

Bourne Shell Family

The Bourne shell family does not really distinguish between shell and environment variables. When a shell starts up, it reads the information in the table of environment variables, defines itself a shell variable for each one, using the same name (also uppercase by convention), and copies the values. From that point on, the shell only refers to its shell variables. If a change is made to a shell variable, it must be explicitly "exported" to the corresponding environment variable in order for any forked subprocesses to see the change. Recall that shell variables are local to the shell in which they were defined.

Shell variables are defined by assignment statements and are unset by the unset command. The format of the assignment statement is:

$ NAME=value; export NAME

where there are no spaces around the equal sign (=). In most cases you will want to include the optional export portion of the command (following the semicolon), but this is optional. The unset command format is:

$ unset NAME

where NAME is the variable name, and value is a character string that is the value of the variable. If the string includes blanks (i.e. if it encompasses multiple values), enclose the string in double quotes, e.g.,

$ NAME="value1 value2 ..."

The values of all the current variables may be displayed with the set command.

To use a variable in a command, preface it with a dollar sign ($). This tells the command interpreter that you want the variable's value, not its name, to be used. For example, to see the value of a single variable, enter:

$ echo $NAME

You can also use ${NAME}, which avoids confusion when concatenated with text.

To prepend or append a value to an existing environment variable, use the following syntax:

$ NAME=prepend_value${NAME}

or

$ NAME=${NAME}append_value

Appending and prepending is commonly used with the PATH variable, and a colon is used as a separator, e.g.,

$ PATH=${PATH}:${XYZ_DIR}