ECN No Name Newsletter: January, 1992

The ECN No Name Newsletter is no longer being published. This is an archived issue.

[previous article] [next article]

Shells, Environment Variables, and Paths

Philip R. Moyer

When users log into a Unix system, they immediately begin interacting with a shell. A shell is a program that runs and interprets the commands users type in. The shell handles commands, terminal characteristics, environment, the prompt you see, and many, many other features.

There are four shells in use at ECN. They are:

In this article, I will call the Bourne shell "sh", the C-shell "csh", the Korn shell "ksh", and Bash "bash." There are some other shells, such as course-specific shells and resh, the restricted shell, but they are not of concern here.

Users can find out what shell they are using by typing "f " where is their own login. One of the entries will be "Shell:", followed by the name of their login shell. Another way users can find out what shell they are using is to type "chsh" . This is the program to change the user's login shell; it will print out which shell the user is using and then ask for the name of their new shell. The program can be interrupted at this point.

Most users have csh as their login shells, unless they have explicitly changed their shell.

Note that the prompt for both sh and ksh is usually "$" and the prompt for csh and bash is usually "%." It is possible, however, to change the prompt from within the shell by setting the appropriate variable; for sh, it is the PS1 variable and for csh it is the "prompt" variable.

Shells support the concept of environment variables. Environment variables are used to configure the shell. They are usually set in the shell's startup script, though they can be set from the command line. The startup script ".login" for both csh and bash. The startup script for both sh and ksh is ".profile," though ksh also supports a ".kshrc" environment file.

In sh and ksh, environment variables are set like normal shell variables, but they must be "exported" before they will be in the user's environment. Setting a default printer, for example, can be accomplished with the PRINTER environment variable:

           $  PRINTER=msd

           $  export PRINTER

In csh and bash, this can be accomplished with one command: setenv. Here is the same example under these other shells:

% setenv PRINTER msd

The format for every environment variable is the same. In sh and ksh, it is

VARIABLE=value

export VARIABLE

and in csh and bash it is

% setenv VARIABLE value

Note that it is possible to set environment variables from the command line as well as the shell initialization scripts (.login, etc.). The syntax is identical.

There are tens, if not hundreds, of environment variables. Here are some common environment variables and a description of what they do.

CDPATH

If a user types "cd" with an argument and that argument is not a valid directory from the current path, cd will attempt to find that directory as a valid path from the directories listed in this environment variable. If the CDPATH is set to ":/home/harbor3/prm:/usr", for example, and I type "cd pub" when pub is not a subdirectory of the current directory, cd(1) will look first in /home/harbor3/prm for the directory pub; if it exists, the current directory will be changed to /home/harbor3/prm/pub. Otherwise, cd(1) will go ahead and look for /usr/pub. This environment variable is not available in sh.

EDITOR

This is the text editor the user likes to use. For example, mine is set to /usr/ucb/vi. It could also be set to ex, emacs, or some other editor. This variable sets the editor the user is given when they edit something from another program, such as rn, the newsreader.

EXINIT

This is the ex (or vi) initialization file. Users may set any ex (vi) options here, such as autoindent or wrapmargin.

HOME

This is the complete path to the home directory. This variable is set by the login program; it is not a good idea to change it.

MANPATH

The man(1) command supports the MANPATH environment variable to allow users to keep their own manual pages. The MANPATH is a colon-separated list of paths to top- level manual pages. If a user uses the X11 windowing system, for example, they should add /usr/ecn/X11/man to their MANPATH. Here is an example of how to do this:

           set MANPATH="/usr/ecn/X11/man:/usr/man"

           export MANPATH

NAME

This is the name of the person who owns the account. It is used by mail systems and news posting software to include the user's real name in any correspondence.

ORGANIZATION

Mailers and news software will also include the user's organization, if the ORGANIZATION environment variable is set. Mine is set to "Purdue University Engineering Computer Network."

PAGER

This is that path to the program used to display files or output that fill more than one page. "/usr/ucb/more" is mine.

PATH

This is perhaps the most important environment variable. See the discussion below.

PRINTER

This is the printer used most often. If this variable is set, lpr(1) will print to this printer if no alternate is specified with the -P option on the command line.

TERM

The terminal the user is currently using. This variable is used to set terminal characteristics.

Note: environment variables traditionally are in all upper-case letters.

See the manual pages for sh and csh for even more environment variables you can use.

Many software packages, as well as Unix itself, check for various environment variables. For example, Autocad Release 11 requires that the ACADSERVER environment variable be set to "ups.ecn.purdue.edu."

The PATH is perhaps the most important environment variable. It determines which directories the shell will search when trying to execute the current command. In sh and ksh, the PATH is a colon-separated list of directories. In csh and bash, it is a space separated list.

The PATH is set just like any other environment variable. Users should be aware of how the PATH works, though.

The PATH environment variable is searched from left to right. If a program is found in a directory, the path is not searched farther. Thus, if there are two versions of a program available (/usr/bin/cc and /usr/lang/cc, for example), users can set their paths to find the desired program first.

The default search path is, in descending search order:

       /usr/ecn
       /usr/ucb
       /bin
       /usr/bin
       /usr/new
       /usr/local
       /usr/custom
       /usr/unsup
       .

The "." in the above list represents the current directory. If you modify your own path, you should NOT, for security reasons, put the current directory at the beginning of your path.

Modifying the path can be quite simple. For example, to add X11 to the path, a user would do this (in sh or ksh):

PATH="/usr/ecn/X11R5/bin:$PATH"

export PATH

if they are using csh or bash, they would use:

setenv PATH ( /usr/ecn/X11R5 $path )

As another example, users who want to use specialized compilers on the Sun workstations at ECN should modify both their PATH and their MANPATH. They should add /usr/lang to the beginning of their PATH and /usr/lang/man to the beginning of their MANPATH. This way, when they type "cc", their shell will find the unbundled C compiler in /usr/lang, rather than the bundled C compiler in /usr/bin.

Remember that each shell may have idiosyncracies that need to be taken into account when you are setting environment variables. For example, users should NOT reset the IFS variable in sh or ksh unless they are absolutely positive they know what they are doing.

For additional information, read the man pages for each shell: sh, csh, bash, and ksh. These manual pages supply valuable information of which users may not be aware.


webmaster@ecn.purdue.edu
Last modified: Wednesday, 04-Mar-98 18:17:29 EST

[HTML Check] HTML