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:
The following MouseOver applet shows how an environment variable created for a specific application might be accessed in a script.
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.