Linux setenv command

Updated: 05/04/2019 by Computer Hope
setenv command

On Unix-like operating systems running the C shell, the setenv built-in command adds, or changes, the value of an environment variable.


setenv VAR [VALUE]


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


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 environment 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"

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


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

echo "$MYVAR"

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:


...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"

And now let's go back to 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.


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

Sets the environment variable PATH. PATH is a list of path names 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.

csh — The C shell command interpreter.
ksh — The Korn shell command interpreter.
set — Set the value of shell options and positional parameters.
sh — The Bourne shell command interpreter.