[Next] [Previous] [Up] [Top] [Contents]

CHAPTER 5 Shells

5.4 The C Shell, csh

Csh uses the startup files .cshrc and .login. Some versions use a system-wide startup file, e.g. /etc/csh.login. Your .login file is sourced (executed) only when you login. Your .cshrc file is sourced every time you start a csh, including when you login. It has many similar features to .profile, but a different style of doing things. Here we use the set or setenv commands to initialize a variable, where set is used for this shell and setenv for this and any subshells. The environment variables: USER, TERM, and PATH, are automatically imported to and exported from the user, term, and path variables of the csh. So setenv doesn't need to be done for these. The C shell uses the symbol, ~, to indicate the user's home directory in a path, as in ~/.cshrc, or to specify another user's login directory, as in ~username/.cshrc.

Predefined variables used by the C shell include:

A simple .cshrc could be:

set path=(/usr/bin /usr/ucb /usr/local/bin ~/bin . ) # set the path

set prompt = "{'hostname' 'whoami' !} " # set the primary prompt; default is "%"

set noclobber # don't redirect output to existing files

set ignoreeof # ignore EOF (^D) for this shell

set history=100 savehist=50 # keep a history list and save it between logins

# aliases

alias h history # alias h to "history"

alias ls "/usr/bin/ls -sbF" # alias ls to "ls -sbF"

alias ll ls -al # alias ll to "ls -sbFal" (combining these options with those for "ls" above)

alias cd 'cd \!*;pwd' # alias cd so that it prints the current working directory after the change

umask 077

Some new features here that we didn't see in .profile are noclobber, ignoreeof, and history. Noclobber indicates that output will not be redirected to existing files, while ignoreeof specifies that EOF (^D) will not cause the login shell to exit and log you off the system.

With the history feature you can recall previously executed commands and re-execute them, with changes if desired.

An alias allows you to use the specified alias name instead of the full command. In the "ls" example above, typing "ls" will result in "/usr/bin/ls -sbF" being executed. You can tell which "ls" command is in your path with the built-in which command, i.e.:

which ls

ls: aliased to /usr/bin/ls -sbF

A simple .login could be:

# .login

stty erase ^H # set Control-H to be the erase key

set noglob # prevent wild card pattern matching

eval 'tset -Q -s -m ':?xterm'' # prompt for the terminal type, assume "xterm"

unset noglob # re-enable wild card pattern matching

Setting and unsetting noglob around tset prevents it from being confused by any csh filename wild card pattern matching or expansion.

Should you make any changes to your startup files you can initiate the change by sourcing the changed file. For csh you do this with the built-in source command, i.e.:

source .cshrc

For further information about csh type "man csh" at the shell prompt.

Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]