Main index

Introducing UNIX and Linux


Processes and devices

Overview
Processes
      Process status
      Foreground and background
      Process control
      Signals
Environment
      Environment variables
      Global and local variables
      Executable scripts
Program control
      Job control
      Command history list
      Running a job at a specific time
      Running programs periodically
      Big programs
      Timing a program
      Running programs in order
Quotes and escapes
Devices
Backquotes
Summary
Exercises

Environment variables

A variable in UNIX is a name associated with a value. For instance, there is a name LOGNAME whose value is your own username. Some systems also have a variable USER with the same value. Variable names are by convention formed of upper-case letters, whereas names of files are normally lower-case. The value of a variable can be referred to by prefixing the name with a $:

echo LOGNAME
LOGNAME
echo $LOGNAME
chris

Some variable names are set by the UNIX system for you; other names you can set for yourself. The syntax for assigning a value to a name is name = value, for instance:

ADDRESS="1 High Street"
echo I live at $ADDRESS
I live at 1 High Street

If the value of a variable includes whitespace (Spaces or TABs) or symbols known to the shell (such as & and |), the value should be enclosed in single or double quotes. For the moment, just think of the value as being a string; if it contains numbers, they are still just sequences of characters, and you will not (yet) be able to do any arithmetic on it. Check the values of these predefined variables:

EDITOR Your preferred editor
HOME The absolute pathname of your home directory
LOGNAME Your login name
PATH The 'search path' for commands
PRINTER The 'default' printer that lpuses
PS1 The shell prompt
PS2 The shell 'continuation' prompt
SHELL The pathname of the shell you use
TERM The type of terminal or window you are using
VISUAL Your preferred full-screen editor (possibly same as EDITOR)

Of these, PATH and PS1 deserve further discussion. When a UNIX shell encounters a command that is not built in to the shell, it looks at the variable PATH - as you may have noticed, the value of this variable is a sequence of pathnames, known as pathname components, separated by colons. UNIX then examines each of these pathnames in order, assuming each to be a directory, to see whether there is an executable file whose name is the same as that of the command. If it finds one, it is executed, otherwise an error message is generated when all the directories in PATH have been examined. Typically, PATH will have been set up on your system so as to contain the directories the system administrator knows you will need; a typical example might be:

echo $PATH
/bin:/usr/bin:/usr/local/bin

For the moment, do not try to reset the value of PATH.

If you want a variable's value to contain the dollar symbol, prefix the dollar with a backslash, or enclose the value in single quotes:

X='This is a $'
echo $X
This is a $

The variable PS1 controls the prompt the shell gives you; you can safely play with this variable:

PS1="Type in a command: "
Type in a command: echo $PS1
Type in a command:

The concept of a variable is understood by any process; a variable can be assigned a value by other utilities, not just by the shell. However, the value of a variable is not automatically available to other processes.

You may ask 'What happens if I change the value of LOGNAME?' Try it - the system will not prevent you from changing it. The only problem to arise is if you run a command that needs to know about LOGNAME, such as one you may have written yourself. The system knows who you are, and does not need to examine LOGNAME to find out - using LOGNAME is an aid to you when writing shell scripts.

You can list all the variables set for you by use of the command env ('environment') with no arguments, which we discuss in more detail later on. Try it - you may need to pipe the output through a pager, since your system may have set many variables for you:

env | more

Worked example

Find out the name of the type of terminal you are using.
Solution: Examine the contents of the environment variable TERM:

echo $TERM
xterm


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck