Csh Environment and Scripts

Working With Unix
[Shell Variables |Environmental Variables |Shell Scripting |Special Files |Exercises ]

Shell Variables

Shell variables are parts of data that the shell keeps in memory. They are used for determining the settings for the shell.

To see what shell variables are set, use the set command. With no options, it prints a list of all of the currently set shell variables.

larry% set
argv    ()
cwd     /home/schallee/tmp/unix/samplefiles
history 40
home    /home/schallee
ignoreeof
lahs    lahs.losalamos.k12.nm.us
lpath   (/usr/openwin/bin/xview /usr/openwin/bin)
mychoice        openwin
noclobber
notify
path    (/home/schallee /usr/openwin/bin/xview /usr/openwin/bin /home/schallee/bin /usr/local/bin /usr/lang /usr/ucb /usr/bin /usr/etc .)
prompt  larry% 
savehist        4000
shell   /bin/csh
status  0
term    xterm
user    schallee
larry% 
You can also set or modify the variables with the set command. 
The syntax for set
is:

set variblename = data
This will set variablename to data.

larry% set toast = "yummy"
larry% set
...
term    xterm
toast   yummy
user    schallee
larry% 
Once a variable is set, you can use it in your shell, by preceding the variable name with a "$":

larry% cp hi yummy
larry% cat $toast
Hi there. How are you?
larry% 

Environmental variables

Environmental variables are just like shell variables, except that they are passed to programs run by the shell, and shell variables aren't. The command for environmental variables is setenv. It works much the way set does except that it does not use an "=" when it sets data. Just like set, to get a list of the currently set variables, use setenv with no arguments:

larry% setenv
DISPLAY=:0.0
FONTPATH=/usr/openwin/lib/fonts
HELPPATH=/usr/openwin/lib/locale:/usr/openwin/lib/help
HOME=/home/schallee
LD_LIBRARY_PATH=/usr/openwin/lib
LOGNAME=schallee
MANPATH=/usr/local/man:/usr/man:/usr/openwin/man:/home/schallee/man
NOSUNVIEW=0
OPENWINHOME=/usr/openwin
PATH=/home/schallee:/usr/openwin/bin/xview:/usr/openwin/bin:/home/schallee/bin:/usr/local/bin:/usr/lang:/usr/ucb:/usr/bin:/usr/etc:.
PGPPATH=/home/schallee/.pgp
PWD=/home/schallee/tmp/unix/samplefiles
SHELL=/bin/csh
TERM=xterm
USER=schallee
WINDOW_PARENT=/dev/win0
WINDOW_TTYPARMS=2,23557,13,13,127,21,3288,3,28,17,19,4,-1,26,25,18,15,23,22
WMGR_ENV_PLACEHOLDER=/dev/win3
XDO=-bg black -fg green
XFILESEARCHPATH=/usr/openwin/lib/%T/%N%S
XINITRC=/usr/openwin/lib/Xinitrc
WINDOWID=11534349
TERMCAP=vs|xterm|vs100|xterm terminal emulator (X window system):li#43:co#82: : :cr=^M:do=^J:nl=^J:bl=^G:le=^H:ho=\E[H:   :co#80:li#65:cl=\E[H\E[2J:bs:am:cm=\E[%i%d;%dH:nd=\E[C:up=\E[A:   :ce=\E[K:cd=\E[J:so=\E[7m:se=\E[m:us=\E[4m:ue=\E[m:     :md=\E[1m:mr=\E[7m:me=\E[m:       :ku=\EOA:kd=\EOB:kr=\EOC:kl=\EOD:kb=^H: :k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:ta=^I:pt:sf=\n:sr=\EM:   :al=\E[L:dl=\E[M:ic=\E[@:dc=\E[P::MT:ks=\E[?1h\E=:ke=\E[?1l\E>:   :is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;3;4;6l:        :rs=\E[r\E<\E[m\E[2J\E[H\E[?7h\E[?1;3;4;6l:xn:    :AL=\E[%dL:DL=\E[%dM:IC=\E[%d@:DC=\E[%dP: :ti=\E7\E[?47h:te=\E[2J\E[?47l\E8:      :hs:ts=\E[?E\E[?%i%dT:fs=\E[?F:es:ds=\E[?E:
SHLVL=1
HOST=larry
HOSTTYPE=sun4
larry% 
If you wanted to set the environmental variable "yummy" to "toast," you would do the following:

larry% setenv yummy toast
larry% setenv
...
HOST=larry
HOSTTYPE=sun4
yummy=toast
larry% 

Shell Scripts

A shell script is a text file that is run by the shell as a list of commands. You can do anything in a shell script that you can do on a command line. You can then use the shell script as another command.

For example, if you wanted to create a command that displayed the date, a regular listing of the current directory, the path do the current directory, and who you are, you could have a text file like in the following example.

larry% cat status
date
ls
pwd
whoami
larry% csh status
Fri Jul 14 14:44:10 MDT 1995
a.out*                          lunch
breakfast                       mp.and.the.holy.grail.txt
hi                              sorted.huge
huge                            status
innocent_victim*                 yummy
/home/schallee/tmp/unix/samplefiles
schallee
larry% 
The file was run by specifying it on the command line for csh. This works, but is not vary fun. A better way to do it is to give your self execute permission on the file. You can do this using chmod:

larry% chmod u+x status
larry% status
Fri Jul 14 14:46:21 MDT 1995
a.out                           lunch
breakfast                       mp.and.the.holy.grail.txt
hi                              sorted.huge
huge                            status
innocent_victim                  yummy
/home/schallee/tmp/unix/samplefiles
schallee
larry% 
This is a lot better then typing csh before status. Shell scripting is really a programming language, and entire books are written on it.

Special Files

There are several special dot files, that is files whose names begin with a "." The three most common ones are the .cshrc, .login, and .logout. These three files are shell scripts that are executed automaticly. The .cshrc file is automaticly run every time the csh shell starts. The .login file is executed once every time you login. The .logout file is executed every time you logout. At login, the .cshrc is processed first when the shell starts up, then the .login file is processed. You can modify these files to suit your self.

Exercises

  1. Display what shell variables you currently have set.
    larry% set
    
  2. Set a new variable.
    larry% set yummy = the
    
  3. Use the new variable as the search pattern for grep.
    larry% grep $yummy mp.and.the.holy.grail.txt
    
  4. Display the environmental variables you have set.
    larry% setenv
    
  5. Set an environmental variable.
    larry% setenv toast yummy
    
  6. Write a simple shell script to print the date, the current directory, and the last two commands that you entered.
    larry% cat stat
    date
    pwd
    history | head -2
    larry% 
    
  7. Run it as a argument to csh.
    larry% csh stat
    
  8. Set it up to be run as, and run it as a command.
    larry% chmod u+x stat
    larry% stat
    

[Back |Contents |Next ]

Edward B. Schaller (schallee@earthlink.net)