- Working with Variables
- Environment and Shell Variables
- Summary
- Terms
Environment and Shell Variables
When the shell starts a program, it passes that program a set of variables called the environment. The environment is usually a small subset of the variables defined in the shell. Each variable in the environment is called an environment variable.
The variables we have examined thus far have been local variables. Local variables are variables whose value is restricted to a single shell. Local variables are not passed to programs started by the shell.
In addition to local variables and environment variables, there is a third category of variables called shell variables. These are special variables set by the shell that are required for proper operation of the shell. Some shell variables are environment variables, whereas others are local variables.
Table 8.1 compares these three categories of variables.
Table 8.1 A Comparison of Local, Environment, and Shell Variables
Attribute |
Local |
Environment |
Shell |
Accessible by child processes |
No |
Yes |
Yes |
Set by users |
Yes |
Yes |
No |
Set by the shell |
No |
No |
Yes |
User modifiable |
Yes |
Yes |
No |
Required by the shell |
No |
No |
Yes |
Exporting Environment Variables
Environment variables are just local variables that have been placed into the environment via the export command:
export name
The variable specified by name is placed in the environment. The process of placing variables into the environment is often referred to as exporting the variable. The standard shell idiom for exporting variables is
name=value ; export name
An example of this is
PATH=/sbin:/bin ; export PATH
Here a value is assigned to PATH, and then PATH is exported. Often, the assignment statement of an environment variable and the corresponding export statement are written on one line to clarify that the variable is an environment variable. This helps the next programmer, who has to maintain the script, quickly grasp the use of certain variables.
A single export command can be used to export more than one variable. For example, the command
export PATH HOME UID
exports the variables PATH, HOME, and UID to the environment.
Exporting Variables in ksh, bash, and zsh
An alternative form for exporting variables is available in ksh, bash, and zsh:
export name=value
In this form, the variable specified by name is assigned the specified value and then that variable is marked for export. In this form command,
export PATH=/sbin:/bin
is equivalent to
PATH=/sbin:/bin ; export PATH
In this form, any combination of name or name=value pairs can be given to the export command. For example, the command
export FMHOME=/usr/frame CLEARHOME=/usr/atria PATH
assigns the specified values to the variables FMHOME and CLEARHOME and then exports the variables FMHOME, CLEARHOME, and PATH.
Shell Variables
Shell variables are variables that the shell sets during initialization and uses internally. Table 8.2 gives a list of the most common shell variables. Some other shell variables are covered in the section "Variable Substitution" in Chapter 9.
Table 8.2 Shell Variables
Variable |
Description |
PWD |
Indicates the current working directory as set by the cd command. |
UID |
Expands to the numeric user ID of the current user, initialized at shell startup. |
SHLVL |
Increments by one each time an instance of bash is started. This variable is useful for determining whether the built-in exit command ends the current session. |
REPLY |
Expands to the last input line read by the read built-in command when it is given no arguments. This variable is not available in Bourne shell. |
RANDOM |
Generates a random integer between 0 and 32,767 each time it is referenced. You can initialize the sequence of random numbers by assigning a value to $RANDOM. If $RANDOM is unset, it loses its special properties, even if it is subsequently reset. This variable is not available in Bourne shell. |
SECONDS |
Each time this parameter is referenced, it returns the number of seconds since shell invocation. If a value is assigned to $SECONDS, the value returned on subsequent references is the number of seconds since the assignment plus the value assigned. If $SECONDS is unset, it loses its special properties, even if it is subsequently reset. This variable is not available in Bourne shell. |
IFS |
Indicates the Internal Field Separator that is used by the parser for word splitting after expansion. $IFS is also used to split lines into words with the read built-in command. The default value is the string, \t\n, where is the space character, \t is the tab character, and \n is the new-line character. |
PATH |
Indicates the search path for commands. It is a colon-separated list of directories in which the shell looks for commands. A common value is PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/ucb |
HOME |
Indicates the home directory of the current user: the default argument for the cd built-in command. |