Section 11: The Execution Environment
CONCEPT: The exact behavior
of commands issued in the shell depends upon the execution environment provided
by the shell.
The Unix shell maintains a set of environment variables that are used
to provide information, like the current working directory, and the type of
terminal being used, to the programs you run. The environment variables are
passed to all programs that are not built in to the shell, and may be consulted,
or modified, by the program. By convention, environment variables are given in
upper case letters.
To view all the environment variables, use the command
- printenv
You can also view a particular environment
variable using the echo command:
- echo $TERM
The above command echos the value of the TERM
environment variable to the standard output.
The creation of the execution environment
When you log in, a sequence of
events establishes the execution environment. The exact sequence of events
depends on the particular flavor of Unix, and also depends upon the default
shell for your account. The following is a description of the login process for
the HP-UX operating system. Other operating systems may differ.
- The getty process
- The getty process provides the login: prompt that you see on the
terminal screen. The getty process reads your username, and invokes the login
program.
- The login program
- The login program receives the username from getty, and prompts you for
your password. Login then consults the system database /etc/passwd to verify
your password. (Note that login will request your password even if there is no
entry in /etc/passwd for the username you've given. That prevents someone from
finding valid usernames by trial and error.) Login turns off terminal echo so
that your password is not displayed on the screen.
Having verified your password, login then uses information in /etc/passwd
to invoke your default shell. If no default shell is specified in the
/etc/passwd entry, login starts the Bourne shell (/bin/sh).
- Shell startup: System login scripts
- When the shell program starts, it reads configuration files called
login scripts to configure the execution environment. On HP-UX, the
file /etc/profile provides initialization parameters for ksh and sh, while the
file /etc/csh.login is used for csh. After the system login scripts are read,
the shell looks for user-specified login scripts.
- Shell startup: User login scripts
- After the system login scripts are read, the shell reads user login
scripts. User login scripts are kept in one's home directory, and are the
means by which one can customize the shell environment. Sh and ksh look for a
file called .profile. Ksh also reads a file defined in the environment
variable ENV. Csh reads a file called .cshrc, and (if it is the login shell),
the file .login.
Important environment variables
Here are descriptions of some of the
most important environment variables, and examples of how some variables can
affect the execution of commands.
- TERM
- The TERM environment variable defines the type of terminal that you are
using. Most Unix systems have a database of terminal types, and the
capabilities of each terminal type.
- PATH
- The PATH variable contains the names of directories to be searched for
programs that correspond to command names. When you issue a command to the
shell, the shell searches sequentially through each directory in the PATH list
until it finds an executable program with the command name you typed.
- USER
- The USER variable contains your username. Any time you access a file or
directory, the access permissions are checked against the value of USER.
- HOME
- The HOME variable contains the name of your home directory. When you issue
the cd command with no directory argument, you will be placed in the directory
defined in the HOME environment variable. The HOME variable is also where the
shell will look for the user login scripts.
- MAIL
- The MAIL variable contains the name of the directory where your incoming
mail is stored. When you start a mail program, the program will look in the
directory stored in the MAIL environment variable for your incoming mail
messages.
- EDITOR
- The EDITOR variable is used by programs that must invoke a text editor to
provide the ability to edit or compose documents. One example is the elm
program, which is used to read and send electronic mail. If you elect to
compose a new mail message while in elm, the elm program will check the
contents of the EDITOR variable to determine which editor to invoke.
- HOST
- The HOST environment variable contains the name of the host machine that
is running your shell program. When you connect to a remote host through
telnet or ftp, the name of your host is relayed to the remote machine, so the
administrators of the remote machine can keep track of who is connecting, and
from where.
Setting environment and shell variables
The exact mechanism for setting
the environment and shell variables depends upon the type of shell you're using.
sh, or ksh
To set an environment variable in sh or ksh, use the
syntax VAR=value;export VAR, where VAR is the name of the environment variable
and value is the value you wish to assign. Do not put spaces on either side of
the equals sign. The export command instructs the shell to propagate the value
of the variable to all programs that are run by the shell. If an environment
variable is reset, but not exported, the change will only apply to the shell
itself. To set the EDITOR variable to the value emacs in ksh or sh, use the
command:
- EDITOR=pico;export EDITOR
It is also possible to unset environment variables, with the unset command.
Unsetting an environment variable removes the definition of the variable.
csh
To set an environment variable in csh, use the setenv command.
The command has the syntax: setenv VARIABLE value. To set the EDITOR variable to
the value emacs in csh, use the command:
- setenv EDITOR pico
For more information about the shell environment, consult the manual page for
the shell you're using.