Previous Table of Contents Next

Module 119
setenv/unsetenv (csh)


The internal setenv command sets an environment variable to a given value. This allows you to use a meaningful name to contain a value. The value can be changed as required. All variables are exported to subshells.

These variables are placed in the shell's environment. The shell maintains two sets of variables, the local variables and the global, or environment, variables. Local variables are only known to the current shell. Environment variables are known by the current shell and all processes started by the current shell.

The unsetenv command removes a variable from the environment. The value and the variable name are discarded.


Following is the general format of the setenv command.

     setenv variable
     setenv variable=value
     unsetenv variable_pattern


The following list describes the arguments that may be passed to the setenv command.

variable A word used to store a value.
value The alpha, numeric, or alphanumeric value stored in a given variable.
variable_pattern A pattern expanded by the shell which removes all variables that match it.
If no argument is given all environment variables are displayed.


Exported variables exist in the environment space of the shell process. When the shell executes a new child process (subprocess) it passes the environment to the new process. The environment consists only of variables that have been exported. By using exported variables you can pass "global" information to any subprocess.

Subprocesses cannot change the environment of a parent process. They can change their environment but the changes cannot be passed back to the parent.

If setenv is executed without any arguments, on some systems, it lists out all exported variables; on others, it returns an error. If you are in a subshell and use setenv with no arguments, only the variables that have been exported in the subshell are displayed. To list all exported variables that were passed to a subshell use the printenv command described in Module 107.

Automatically Exported Variables

The commonly used csh (local) variables path, term, and user are automatically exported to the environment variables PATH, TERM, and USER. This allows you to perform a set variable=value or a setenv VARIABLE value.


Refer to the set command described in Module 118 and the printenv command in Module 107.


The setenv command is used to allow information to be passed from a parent process to a child process. For example, the vi editor uses the environmental SHELL and TERM variables to know what type of terminal it is working with and which shell to invoke when you request one.


In this activity you use the setenv command to place the COLUMNS variable in the environment. You then start a subshell and view the exported commands using setenv. Begin at the shell prompt. The setenv command places these and other variables in the environment that is passed to other processes.

1.  Type printenv | col and press Return. Notice all of your environment variables are displayed.
2.  Type setenv COLUMNS 80 and press Return. No response is sent to your terminal.
3.  Type printenv | col and press Return. Notice now the COLUMNS variable is in the environment.
       cj> printenv
4.  Turn to Module 145 (SV), Module 107 (BSD) to continue the learning sequence.

Previous Table of Contents Next