TCSH / CSH: set vs setenv Command Differences

by on August 12, 2012 · 2 comments· LAST UPDATED August 12, 2012

in , , ,

I've recently started to use FreeBSD operating system with tcsh (c shell clone) shell. I noticed that both set and setenv command set shell user environment. What's the difference between the SET and SETENV commands?

Tutorial details
DifficultyEasy
Root privilegesNo
RequirementsC-shell

C Shell: set vs setenv

The c shell (tcsh or csh) sits between you and the operating system. It act as a command interpreter. It reads your command and translates the commands into actions taken by the Unix system. When the c shell starts up it reads its startup files and may set environment variables using setenv command.

All variables set with setenv command are automatically exported to subshell. All csh variables set to with set command are NOT automatically exported to subshell. In this example, set an enviorment variable called vech using set command:

 
set vech=bus
echo "$vech"
 

Start a new shell
csh
Try to display $vech, enter:

 
echo "$vech"
 

Sample outputs:

vech: Undefined variable.

Now, try same with setenv command:

 
setenv jaildir /nas/httpd
echo "$jaildir"
csh
echo "$jaildir"
 

Sample outputs:

/nas/httpd

In short, set command is used for this shell and setenv for this and any subshells. Usually, all system environmental variable such as $HOME, $USER, $MAIL, $PATH, and others are defined using setenv command.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 2 comments… read them below or add one }

1 Colin August 13, 2012 at 1:13 am

So does setenv work with all shells or just csh?

Reply

2 Chris F.A. Johnson August 14, 2012 at 1:25 pm

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: