Unix / Linux: TCSH / CSH Shell Set Variable

by on August 6, 2010 · 0 comments· LAST UPDATED March 5, 2014

in , ,

How do I set environment variables using c shell (tcsh or csh) under FreeBSD UNIX or Linux operating systems? How do I save variable using csh shell under Unix like operating systems?

You can use the set or setenv command under c shell (tcsh/csh) to set an environment variable.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time2m
The c shell use the following syntax to set an environment variables and mostly used for

  1. Setting or modifying PATH.
  2. Set your default (preferred) editor.
  3. Set pager and more.

setenv command syntax

If you use csh or tcsh, at the shell prompt, enter:

 
setenv var value
 

In this example, set an environment variables in the csh (and tcsh) called vech to bus:

 
setenv vech bus
 

To display the value for $vech, enter:

 
echo "$vech"
 

OR

 
printf "%s\n" "$vech"
 

In this example, modify your path, enter:

 
setenv PATH $PATH\:/dir/path
setenv PATH $PATH\:/usr/local/app1/bin
 

To see all variables, type:

 
setenv
 

Sample outputs:

_=/bin/csh
OLDPWD=/home/vivek
PWD=/home/vivek
SHLVL=2
USER=vivek
LOGNAME=vivek
HOME=/home/vivek
MAIL=/var/mail/vivek
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/home/vivek/bin
TERM=xterm
FTP_PASSIVE_MODE=YES
BLOCKSIZE=K
SHELL=/usr/local/bin/zsh
SSH_CLIENT=192.168.1.5 57038 22
SSH_CONNECTION=192.168.1.5 57038 192.168.1.109 22
SSH_TTY=/dev/pts/0
HOSTTYPE=FreeBSD
VENDOR=unknown
OSTYPE=FreeBSD
MACHTYPE=unknown
GROUP=vivek
HOST=fdbsd9
REMOTEHOST=192.168.1.5
EDITOR=vi
PAGER=more

set command syntax

The syntax is:

 
set var=val
set foo=bar
echo "$foo"
 

~/.cshrc file

You can setenv and other commands in a $HOME/.cshrc file. This file is located in your home directory that is read whenever a c shell starts. The .login file is read after the .cshrc file when this is a login script. In this example, set default pager to most and add to ~/.cshrc file
echo 'setenv PAGER most' >> $HOME/.cshrc

csh: set vs setenv

You use the set or setenv commands to initialize a variable, where set is used for current shell and setenv for current and any subshells (i.e. it will automatically export variables to subshell). setenv should be used for PATH, HOME, and all other system related environmental settings. Use set command in your csh script only.

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

{ 0 comments… add one now }

Leave a Comment

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

Previous Faq:

Next Faq: