Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Environment Variables

When a shell is running, three main types of variables are present:

  Local Variables
  Environment Variables
  Shell Variables


A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. The variables that you looked at previously have all been local variables.


An environment variable is a variable that is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually a shell script defines only those environment variables that are needed by the programs that it runs.


A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.

Table 7.1 gives a summary of the different types of variables discussed in this section. This table compares local variables set by the user, environment variables set by the user, and shell variables set by the shell.

Table 7.1 A Comparison of Local, Environment, and Shell Variables

Attribute Local Environment Shell

Accessible by No Yes Yes
child processes
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


You place variables in the environment by exporting them. Exporting can be done as follows:
export name

This command marks the variable with the specified name for export. This is the only form supported by sh, thus it is the most commonly encountered form. The standard shell idiom for exporting environment variables is

name=value ; export name

An example of this is

PATH=/sbin:/bin ; export PATH

Here you set the value of the variable PATH and then export it. Usually 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.

You can also use the export command to export more than one variable to the environment. For example,

export PATH HOME UID

exports the variables PATH, HOME, and UID to the environment.

Exporting Variables in ksh and bash

A second form for exporting variables is supported by ksh and bash:

export name=value

In this form, the variable specified by name is assigned the given value. Then that variable is marked for export. In this form, you can write the previous example as

export PATH=/sbin:/bin

In bash and ksh, 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 given values to the variables FMHOME and CLEARHOME and then exports the variables FMHOME, CLEARHOME, and PATH.


Previous Table of Contents Next