next up previous contents
Next: Variable substitution (set, echo) Up: The Shell (csh, tcsh, Previous: Creating aliases (alias)   Contents

Environment variables (setenv, printenv)

Each of the shells has some special-use variables called ``environment'' variables. These are reserved words, often in upper case, which various utilities, and the shell itself, will look for and use if they are defined. By setting up these special environment variables, you can save yourself a great deal of typing, and tailor your environment much more to your liking. Each shell uses different names for different purposes. The discussion here is aimed at the C-shells, although it has some relevance to the Bourne and Korn shells. If you are using one of those, you should check in the man page for your shell to find shell-specific details.

Some environment variables will be defined for you on login; to see what is set for you at any time, use the ((T)C-shell) command printenv. When initializing or changing one, use setenv or, for Bourne or Korn shells, just set (these shells do not distinguish between environment and ordinary variables in the way they are set up).

setenv VARIABLE string         (C-shells)

You can clear whatever you have defined using unsetenv.

unsetenv VARIABLE         (C-shells)

One of the most important environment variables is PATH. PATH is a list of directories the system should search in order to find almost every command you will type. Only a few dozen of the most commonly used commands, such as cd, logout, and execution control commands like if and while are actually part of the shell (called ``built-in'' commands). Most of the rest are really programs the shell should run, such as ls, pwd, etc. It is therefore necessary that the system know where to look, and you can tell it which places it should search for the command by setting up your PATH environment variable appropriately. When you log in, the C-shells will make a table of known commands by searching these directories, and from then on you need only type the command name (Bourne and Korn shells use a different mechanism, but the end result for you is the same). If you later wish to add another directory to your PATH, or you put a new program in one of the directories in your PATH, you will need to use the (TC-shell) command rehash to rebuild this table. If you do not do this, or if you wish to use a utility which is somewhere outside of PATH, you will have to type by hand the whole pathname of the file, e.g. /thisdir/thatdir/somecommand; or give enough information relative to your current directory for the shell to find it, e.g. ../../otherplace/somecommand.

Many UNIX utilities look for environment variables to control how they execute. For example, if you almost always use a particular printer, you can set the environment variable called PRINTER to be the name of your favorite queue, and lp will use it (you can override it by specifying another queue in the lp command). These special variables will always be mentioned in the relevant man page. If you wish to use the value of an environment variable in a command, preface the name with a $, e.g. $PATH. The shell will then substitute the value and use it instead.


next up previous contents
Next: Variable substitution (set, echo) Up: The Shell (csh, tcsh, Previous: Creating aliases (alias)   Contents
h.stilmack@jach.hawaii.edu