Linux and Unix setenv command

Quick links

About setenv
Syntax
Examples
Related commands
Linux and Unix main page

About setenv

In the C shell, setenv adds, or changes the value of an environment variable.

Syntax

setenv VAR [VALUE]

Arguments

VAR The name of the variable to be set.
VALUE The value of the variable, as either a single word or a quoted string.

Description

setenv is a built-in function of the C shell (csh). It is used to define the value of environment variables.

If setenv is given no arguments, it displays all environment variables and their values. If only VAR is specified, it sets an environment variable of that name to an empty (null) value. If both VAR and VALUE are specified, it sets the variable named VAR to the value VALUE. setenv is similar to the set command, which also sets an envirnment variable's value. However, unlike set, setenv also "exports" this environment variable to any subshells. In this way, it is the equivalent of the bash command export.

For instance, if you are inside the c shell, and you use setenv to set the following variable:

setenv MYVAR myvalue

We can then use the echo command to view the value of that variable:

echo "$MYVAR"
myvalue

Our value, "myvalue", was returned. Now let's run bash as a subshell:

bash

and see if it knows the value of our variable MYVAR:

echo "$MYVAR"
myvalue

As you can see, the value of MYVAR was passed on to bash.

Now, let's see how set is different. Let's go back to csh by exiting the bash subshell:

exit

...and use set to set another environment variable, MYVAR2:

set MYVAR2=myvalue2

(The syntax of set, as you can see, is slightly different. It uses an equals sign to assign a value.) Now let's check the value of MYVAR2:

echo "$MYVAR2"
myvalue2

And now let's go back to bash:

bash

...and check the value of MYVAR2:

echo "$MYVAR2"

This time, no value is reported, because the variable was not "exported" to the subshell. So, when you are using csh, if you want environment variables to remain local to only the current shell, use set. If you want them to carry over to subshells as well, use setenv.

Examples

setenv PATH "/bin:/usr/bin:/usr/sbin:/usr/local/bin"

Sets the environment variable PATH. PATH is a list of pathnames separated by colons (":"), which are the default paths to search for executable files when a command is called. After you set PATH to the above value, the shell will look in the paths /bin, /usr/bin, /usr/sbin, and /usr/local/bin, in that order, for the executable files of any subsequent commands that you run.

Related commands

csh — The C shell command interpreter.
ksh — The Korn shell command interpreter.
set — Set the value of an environment variable.
sh — The Bourne shell command interpreter.