Start-up and termination: A C-shell starts by executing the commands in .cshrc. If it is a login shell, it then executes the commands in .login. When a login shell terminates it executes the commands from .logout.
Shell metacharacters: Metacharacters & | ; < > ( and ) can be escaped with \. \ followed by a newline is equivalent to a space. Strings are made with single quotes, double quotes or back-quotes: see below.
Filename completion: If the variable filec is set, type ESC to complete a file name. If several matching file names are present, only the unambiguous part is completed. Type ^D to see all file names that match the given prefix.
Variables: The C-shell has local variables and environment variables. Type
set | to show all local variables, | |
set fred | to set variable fred to the null string, | |
set fred = grand | to set fred to the string grand. |
set bill = (grand blond stupide)To refer to a value, use $fred or $bill[3]. Values in a list are numbered starting from 1. Several list elements can be selected: $bill[1-2]. The number of values in the variable bill is given by $#bill. There are several predefined local variables, including $< which gets the next line of standard input. The variable argv holds the arguments of the script being executed. Now for example $2 denotes the second argument of the script, while $* holds all the arguments except $0 (which is the name of the script).
Environment variables always hold exactly one value. Their names are usually in upper case. For example
setenv DAVE beauThe predefined environment variables include $HOME and $PATH. The environment variables are available to any process invoked by the shell.
Local variables can be split into fields with the aid of modifiers :h (head), :tÊ(tail), :e (extension) and :r (root). For example, if we
set fred = /abc/def/ghk.mnothen $fred:h is /abc/def, $fred:t is ghk.mno, $fred:e is mno and, and $fred:r is /abc/def/ghk. These modifiers cannot be used on environment variables. If a variable holds more than one value, they apply only to the first value. In this case corresponding modifiers :gh, :gt, :ge and :gr apply to every value in the variable.
Expressions: The C-shell supports most of C's operators, except that they only work on integers. To assign an expression to a variable, use @:
set a = 3 set b = 7 if ($a < $b) then echo All is well endif @ c = $a + $bTests on files: A series of tests on files have the form -option fileName, for example
-e fileName | true if fileName exists, | |
-d fileName | true if fileName is a directory, and | |
-w fileName | true if the shell has write permission on fileName. |
set history = 50It is convenient if the shell displays command numbers. Use
set prompt = '\! %'To redisplay the last 5 commands, type
history 5To re-execute an earlier command, type for example
!! | re-execute the previous command, | |
!23 | re-execute command number 23, | |
!str | re-execute the last command that started with str, or | |
^old^new | re-execute the previous command after replacing the string old by the string new. For example cat fileaNme.txt ; ^aN^Na |
!!:0 | selects the first word of the previous command, | |
!23:$ | selects the last word of command number 23, | |
!str:* | selects all but the first word of the last command starting str, and | |
!!:2-3 | selects words 2 and 3 of the previous command. |
Aliases: The alias command lets you define new commands. For example
alias | shows all defined aliases, | |
alias fred | shows the definition of alias fred, while | |
alias fred 'ls -al' | defines fred to mean ls -al. |
alias rm 'rm -i'However any form of recursive definition must be avoided. Parts of the command line can be selected using the same selectors as for selecting parts of commands in the history. The selector must be preceded by \! For example, with
alias lm 'ls \!* | more'the command
lm bill fred jimis executed as
ls bill fred jim | morebecause \!* selects all but the first word of the command.
Substitutions: Before a command is executed, the following substitutions are made:
echo Today is `date` echo There are `who | wc -l` users on the systemUsing single quotes prevents variable substitution, wildcard replacement, and command substitution. Using double quotes prevents wildcard replacement only. If quotes are nested, only the outermost pair have any effect.
Sequences and groups: If a series of commands is separated by && the next command is executed only if the previous command had exit code 0 (which conventionally means it worked correctly); if a series is separated by || the next command is executed only if the previous command had a non-zero exit code.
cc -o myprog myprog.c && myprog cc -o badprog badprog.c || echo failed to compileCommands may be grouped by putting them in parentheses, and then standard input and output can be redirected for the whole group, or the group can be run in the background:
(date ; ls ; pwd) >out.txt (date ; ls ; pwd) &Subshells:
Job control: The shell associates a numbered job with each command sequence to keep track of those commands that are running in the background or have been stopped with ^Z. When a command is started in the background, the shell displays a line with the job number in brackets and the associated PID:
[1] 1234To see the current list of jobs, use the jobs command. To manipulate jobs the following commands are available:
bg %n | move job n to the background, | |
fg %n | move job n to the foreground), | |
kill %n | kill job n | |
kill PID | kill the job with the given PID | |
stop %n | stop job n without killing it, and | |
wait | wait until all subshells have finished. |
The C-shell does not automatically kill background jobs when you log out. To see if you have jobs still running from a previous session, type
ps -fu $USERThe command kill can be used to send other signals to a job:
kill -USR1 %nsends the signal USR1 to job n. For the list of available signals, do
kill -lScripts: Any series of shell commands can be stored in a file. There are two ways of executing them. The command
source fileNamereads commands from the file fileName and executes them without creating a subshell. Otherwise
fileName arg1 arg2 ... argncreates a subshell to execute the commands in fileName. The file in question must have execute permission. The arguments are available in the subshell as variables $0 (the name of the script), $1, and so on. If the first line of the script has the form #!pathName then the named program is used to execute the script; otherwise if the first character of the script is # then a new C-shell is invoked; and if neither of these conditions holds the script is executed by a Bourne shell. In general the character # starts a comment which extends to the end of the line.
Control structures: The C-shell offers a variety of control structures. It is always safest to put the keywords foreach, end, and so on on separate lines as shown.
repeat count commandThe command is repeated count times.
foreach var (wordlist) commandList endThe variable var is set successively to each value in the wordList, and the list of commands is executed for each new value of var.
Commands break and continue work as in C.
if (expr) then commandList else if (expr) then commandList ... else commandList endifWorks as you would expect.
if (expr) commandThe command must be a single, simple command, not a pipeline.
switch (string) case pattern: commandList breaksw ... default: commandList breaksw endswEach pattern is successively matched against the specified string. If no pattern matches, the default command list is used. If a breaksw command is absent, control falls through to the next case.
while (expr) commandList endCommands break and continue work as in C.
exit exit (expr)Return to the parent shell. The termination code is either that of the last command executed, or the given expression.
# Move files to $HOME/trash, from where they can be # removed by commands in .logout. If $HOME/trash does # not exist, it is created. # # Call: trash filelist # if (! (-e $HOME/trash)) then pushd $HOME >/dev/null # Move to home directory mkdir trash # Create trash directory popd >/dev/null # Return to original directory endif foreach var ($argv) if (-e $var) then mv $var $HOME/trash else echo No such file: $var endif endThe commands pushd and popd allow you to change directories and then return. Their output is suppressed by redirecting it to /dev/null.
# Call: tgif # set date = (`date`) switch ($date[1]) case Fri: echo Thank goodness it\'s Friday! breaksw case Thu: echo It\'s Friday tomorrow! breaksw default: echo It\'s still a long time until Friday. breaksw endsw