UNIX Tutorial Seven

8.1 UNIX Variables

Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Some are set by the system, others by you, yet others by the shell, or any program that loads another program.

Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session. By convention, environment variables have UPPER CASE and shell variables have lower case names.

8.2 Environment Variables

An example of an environment variable is the OSTYPE variable. The value of this is the current operating system you are using. Type

$ echo $OSTYPE

More examples of environment variables are

Finding out the current values of these variables.

ENVIRONMENT variables are set using the setenv command, displayed using the printenv or env commands, and unset using the unsetenv command.

To show all values of these variables, type

$ printenv | less

8.3 Shell Variables

An example of a shell variable is the history variable. The value of this is how many shell commands to save, allow the user to scroll back through all the commands they have previously entered. Type

$ echo $history

More examples of shell variables are

Finding out the current values of these variables.

SHELL variables are both set and displayed using the set command. They can be unset by using the unset command.

To show all values of these variables, type

$ set | less

So what is the difference between PATH and path ?

In general, environment and shell variables that have the same name (apart from the case) are distinct and independent, except for possibly having the same initial values. There are, however, exceptions.

Each time the shell variables home, user and term are changed, the corresponding environment variables HOME, USER and TERM receive the same values. However, altering the environment variables has no effect on the corresponding shell variables.

PATH and path specify directories to search for commands and programs. Both variables always represent the same directory list, and altering either automatically causes the other to be changed.

8.4 Using and setting variables

Each time you login to a UNIX host, the system looks in your home directory for initialisation files. Information in these files is used to set up your working environment. The bash shell uses a file called .profile (note that the file name begins with a dot).

At login the bash shell reads .profile and performs the actions listed in the file.

8.5 Setting shell variables in the .profile file

For example, to change the format of your shell prompt, use

$ PS1="\u@\h:\w\$ "

Check this has worked by moving into directory unixstuff and observing how the shell prompt changes as you navigate through your file system. Recall that no matter where you are, typing in cd will get you back to your home directory.

However, this has only set the variable for the lifetime of the current shell. If you open a new xterm window, it will only have the default history value set. To PERMANENTLY set the value of history, you will need to add the command above to the .profile file.

First open the .profile file in a text editor. A simple editor to use is pico.

$ pico ~/.profile

Add the following line AFTER the list of other commands.

PS1="\u@\h:\w\$ "

Save the file (by typing in CTRL-X, then Enter) and force the shell to reread its .profile file by using the shell source command.

$ source ~/.profile

Check this has worked by navigating your file system with the cd command.

8.6 Setting the path

When you type a command, your path (or PATH) variable defines in which directories the shell will look to find the command you typed. If the system returns a message saying "command: Command not found", this indicates that either the command doesn't exist at all on the system or it is simply not in your path.

For example, to run the gcc compiler, you either need to directly specify the gcc path (/opt/gnu/bin/gcc), or you need to have the directory /opt/gnu/bin/ in your path.

You can add it to the end of your existing path (the $PATH represents this) by issuing the command:

$ PATH=/opt/gnu/bin:$PATH

Test that this worked by invoking the command.

$ which gcc

The directory /opt/gnu/bin/ should be listed.

HINT: You can run multiple commands on one line by separating them with a semicolon.

To add this path PERMANENTLY, add the following line to your .profile just before the export $PATH command.

PATH=/opt/gnu/bin:$PATH

M.Stonebank@surrey.ac.uk October 2001