Shell Variables   «Prev  Next»
Lesson 4Accessing environment variables
ObjectiveUse environment variables within a script.

Accessing Environment Variables in Unix Shell Scripts

Purpose of Environment Variables

Environment variables are used to store information that needs to be passed between different programs working within the same environment. When you log in to UNIX, the shell sets many environment variables. These include your username (variable USER) and your home directory (variable HOME).
Some environment variables are used for specific applications. The installation program that installs the application sets up these variables. Examples include database programs or graphical programs that need to know which directories to use for locating files. These programs expect to have environment variables set to indicate this information. This also enables you to customize your environment rather than having all details of a program fixed and unchangeable.
Environment variable: Name-value pairs containing values that are needed (and can be accessed) by any program within the environment simply by querying for the value of the environment variable's name.

Accessing environment variables

You can access an environment variable within a script by using the name of that variable, just as you do with other variables. The difference is that the environment variable is not defined or set in your script. The shell itself defines the variable for you. One example of an environment variable that the shell defines is HOME. For example, if you want commands in your script to examine the home directory of the user who ran the script, you can include this command:

cd $HOME

Or, if you want to issue a greeting to the user (using the user’s account name), you can use this command:
echo Welcome to the database conversion utility, $USER

Viewing all environment variables

You can see all of the environment variables that are defined in your current working environment by using the command set.

set

If you use this command within a shell script, you will see a different set of environment variables than at a standard command line. This is because the environment for running the script is established in a different way than a regular command-line environment.

Defining new environment variable

You can define additional variables in your shell scripts, but when a shell script is started, it uses a “sub-environment” that does not pass information back to the shell that launched the script. For this reason, you should add additional environment variables that are needed by your programs to the UNIX startup scripts, such as /etc/profile or .profile in your home directory. By doing so, the environment variables that you add will be defined (and available to all programs and shell scripts) as long as you are logged in to the UNIX system.
To add environment variables to your startup scripts, use the export command with the name of a variable that you have defined:

DATABASE_DIR=/usr/local/database
export DATABASE_DIR

The following MouseOver applet shows how an environment variable created for a specific application might be accessed in a script.
environment Variable
  1. Describe the purpose of the script. The comments should include information about how to use the script, who wrote it, etc
  2. Execute the pwd command and store the string that is returned in the user-defined variable named ARCHIVE_DIR.
  3. Change to the directory defined by the DB_DIR variable, which the user must have defined before running this script. You could also test that this variable exists before using this command.
  4. Copy the new file from the directory in which the script was launched to the DB_DIR directory, represented by a period (the current directory after the cd command line on 3).
  5. Unpack the new archive file using the UNIX tar command.
  6. Change back to the directory in which the script was originally launched by the user.
Describe the purpose of the script. The comments should include information about how to use the script, who wrote it, etc.
Using Environment Variables

Shell Environment Variables-Quiz

Click the quiz link below to test what you have learned about variables so far.
Shell Environment Variables - Quiz
The next lesson describes how to use your own variables in scripts.