TCSH / CSH: set vs setenv Command Differences

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?

ADVERTISEMENTS

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.