EXPERIMENT NO.
3
Implement setting of global and local environment variable, shell
environment variables.
The bash shell uses a feature called environment variables to store information about the shell session and
the working environment (thus the name environment variables). This feature also allows you to store
data in memory that can be easily accessed by any program or script running from the shell. This is a
handy way to store persistent data that identifies features of the user account, system, shell, or anything
else you need to store.
There are two types of environment variables in the bash shell:
■ Global variables
■ Local variables
Global environment variables
Global environment variables are visible from the shell session, and any child processes that the shell
spawns. Local variables are only available in the shell that creates them. This makes global environment
variables useful in applications that spawn child processes that require information from the parent
process.
The Linux system sets several global environment variables when you start your bash session. The system
environment variables always use all capital letters to differentiate them from normal user environment
variables.
To view the global environment variables, use the printenv command:
$ printenv
To display the value of an individual environment variable, use the echo command. When ref-erencing
an environment variable, you must place a dollar sign before the environment variable name:
$ echo $HOME /home/rich
$
Global environment variables are also available to child processes running under the current shell
session:
$ bash
$ echo $HOME /home/rich
$
Local environment variables
Local environment variables, as their name implies, can be seen only in the local process in which they
are defined. In fact, the Linux system also defines standard local environment variables for you by
default.
The set command displays all of the environment variables set for a specific process. However, this also
includes the global environ-ment variables.
You’ll notice that all of the global environment variables seen from the printenv command appear in the
output from the set command. However, there are quite a few additional environment variables that now
appear. These are the local environment variables.
Setting Environment Variables
You can set your own environment variables directly from the bash shell.
Setting local environment variables
Once you start a bash shell (or spawn a shell script), you’re allowed to create local variables that are
visible within your shell process. You can assign either a numeric or a string value to an environment
variable by assigning the variable to a value using the equal sign:
$ test=testing $ echo $test testing
$
If you need to assign a string value that contains spaces, you’ll need to use a single quotation mark to
delineate the beginning and the end of the string:
$ test=testing a long string -bash: a: command not
found
$ test=’testing a long string’ $ echo $test
testing a long string
$
This is a standard convention in the bash shell. If you create new environment variables, it is
recommended (but not required) that you use lower-case letters. This helps distinguish your personal
environment variables from the scores of system environment variables.
Once you set a local environment variable, it’s available for use anywhere within your shell process.
However, if you spawn another shell, it’s not available in the child shell:
$ bash
$ echo $test
$ exit exit
$ echo $test
testing a long string
$
Similarly, if you set a local environment variable in a child process, once you leave the child process
the local environment variable is no longer available:
$ bash
$ test=testing $ echo $test testing
$ exit exit
$ echo $test
$
The test environment variable set in the child shell doesn’t exist when I go back to the parent shell.
Setting global environment variables
Global environment variables are visible from any child processes created by the process that sets the
global environment variable. The method used to create a global environment variable is to create a local
environment variable, then export it to the global environment.
This is done by using the export command:
$ echo $test
testing a long string $ export test
$ bash
$ echo $test
testing a long string
$
Removing Environment Variables
Of course, if you can create a new environment variable, it makes sense that you can also remove an
existing environment variable. This is done by using the unset command:
$ echo $test testing
$ unset test $ echo $test
$
When referencing the environment variable in the unset command, remember not to use the dollar sign.
If you’re in a child process and unset a global environment variable, it only applies to the child process.
The global environment variable is still available in the parent process:
$ test=testing $ export test $ bash
$ echo $test testing
$ unset test
$ echo $test
$ exit exit
$ echo $test testing
$
Setting the PATH Environment Variable
The PATH environment variable seems to cause the most problem on Linux systems. It defines where the
shell looks for commands you enter on the command line. If it can’t find the command, it produces an
error message:
$ myprog
-bash: myprog: command not found
$
The problem is that often applications place their executable programs in directories that aren’t in the
PATH environment variable. The trick is ensuring that your PATH environment variable includes all of
the directories where your applications reside.
You can add new search directories to the existing PATH environment variable without having to rebuild
it from scratch. The individual directories listed in the PATH are separated by a colon. All you need to do
is reference the original PATH value, and add any new directories to the string.
This looks something like this:
$ echo $PATH /usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/bin:/usr/bin:/home /rich/bin
$ PATH=$PATH:/home/rich/test
Variable Arrays
A really cool feature of environment variables is that they can be used as arrays. An array is a variable
that can hold multiple values. Values can be referenced either individually or as a whole for the entire
array.
To set multiple values for an environment variable, just list them in parentheses, with each value
separated by a space:
$ mytest=(one two three four five)
$
Not much excitement there. If you try to display the array as a normal environment variable, you’ll be
disappointed:
$ echo $mytest one
$
Only the first value in the array appears. To reference an individual array element, you must use a
numerical index value, which represents its place in the array. The numeric value is enclosed in square
brackets:
$ echo ${mytest[2]} three
To display an entire array variable, you use the asterisk wildcard character as the index value:
$ echo ${mytest[*]} one two three four five
$
You can also change the value of an individual index position:
$ mytest[2]=seven $ echo ${mytest[*]}
one two seven four five
$
You can even use the unset command to remove an individual value within the array, but be careful, as
this gets tricky. Watch this example:
$ unset mytest[2]
$ echo ${mytest[*]}