CSH(1) UNIX Reference Manual CSH(1)
NAME
csh - a shell (command interpreter) with C-like syntax
SYNOPSIS
csh [-bcefinstvVxX] [arg ...]
DESCRIPTION
The Csh is a command language interpreter incorporating a history mecha-
nism (see History Substitutions), job control facilities (see Jobs), in-
teractive file name and user name completion (see File Name Completion),
and a C-like syntax. It is used both as an interactive login shell and a
shell script command processor.
Argument list processing
If the first argument (argument 0) to the shell is `-' then this is a lo-
gin shell. The flag arguments are interpreted as follows:
-b This flag forces a ``break'' from option processing, causing any
further shell arguments to be treated as non-option arguments.
The remaining arguments will not be interpreted as shell options.
This may be used to pass options to a shell script without confu-
sion or possible subterfuge. The shell will not run a set-user ID
script without this option.
-c Commands are read from the (single) following argument which must
be present. Any remaining arguments are placed in argv.
-e The shell exits if any invoked command terminates abnormally or
yields a non-zero exit status.
-f The shell will start faster, because it will neither search for
nor execute commands from the file .cshrc in the invoker's home
directory.
-i The shell is interactive and prompts for its top-level input, even
if it appears to not be a terminal. Shells are interactive with-
out this option if their inputs and outputs are terminals.
-n Commands are parsed, but not executed. This aids in syntactic
checking of shell scripts.
-s Command input is taken from the standard input.
-t A single line of input is read and executed. A `\' may be used to
escape the newline at the end of this line and continue onto an-
other line.
-v Causes the verbose variable to be set, with the effect that com-
mand input is echoed after history substitution.
-x Causes the echo variable to be set, so that commands are echoed
immediately before execution.
-V Causes the verbose variable to be set even before .cshrc is exe-
cuted.
-X Is to -x as -V is to -v.
After processing of flag arguments, if arguments remain but none of the
-c, -i, -s, or -t options were given, the first argument is taken as the
name of a file of commands to be executed. The shell opens this file,
and saves its name for possible resubstitution by `$0'. Since many sys-
tems use either the standard version 6 or version 7 shells whose shell
scripts are not compatible with this shell, the shell will execute such a
`standard' shell if the first character of a script is not a `#', i.e. if
the script does not start with a comment. Remaining arguments initialize
the variable argv.
An instance of csh begins by executing commands from the file
/etc/csh.cshrc and, if this is a login shell, /etc/csh.login. It then ex-
ecutes commands from .cshrc in the home directory of the invoker, and, if
this is a login shell, the file .login in the same location. It is typi-
cal for users on crt's to put the command ``stty crt'' in their .login
file, and to also invoke tset(1) there.
In the normal case, the shell will begin reading commands from the termi-
nal, prompting with `% '. Processing of arguments and the use of the
shell to process files containing command scripts will be described lat-
er.
The shell repeatedly performs the following actions: a line of command
input is read and broken into words. This sequence of words is placed on
the command history list and parsed. Finally each command in the current
line is executed.
When a login shell terminates it executes commands from the files .logout
in the user's home directory and /etc/csh.logout.
Lexical structure
The shell splits input lines into words at blanks and tabs with the fol-
lowing exceptions. The characters `&' `|' `;' `<' `>' `(' `)' form sepa-
rate words. If doubled in `&&', `||', `<<' or `>>' these pairs form sin-
gle words. These parser metacharacters may be made part of other words,
or prevented their special meaning, by preceding them with `\'. A new-
line preceded by a `\' is equivalent to a blank.
Strings enclosed in matched pairs of quotations, `', ``' or `"', form
parts of a word; metacharacters in these strings, including blanks and
tabs, do not form separate words. These quotations have semantics to be
described subsequently. Within pairs of `'' or `"' characters a newline
preceded by a `\' gives a true newline character.
When the shell's input is not a terminal, the character `#' introduces a
comment which continues to the end of the input line. It is prevented
this special meaning when preceded by `\' and in quotations using ``',
`'', and `"'.
Commands
A simple command is a sequence of words, the first of which specifies the
command to be executed. A simple command or a sequence of simple com-
mands separated by `|' characters forms a pipeline. The output of each
command in a pipeline is connected to the input of the next. Sequences
of pipelines may be separated by `;', and are then executed sequentially.
A sequence of pipelines may be executed without immediately waiting for
it to terminate by following it with an `&'.
Any of the above may be placed in `(' `)' to form a simple command (which
may be a component of a pipeline, etc.) It is also possible to separate
pipelines with `||' or `&&' indicating, as in the C language, that the
second is to be executed only if the first fails or succeeds respective-
ly. (See Expressions.)
Jobs
The shell associates a job with each pipeline. It keeps a table of cur-
rent jobs, printed by the jobs command, and assigns them small integer
numbers. When a job is started asynchronously with `&', the shell prints
a line which looks like:
[1] 1234
indicating that the job which was started asynchronously was job number 1
and had one (top-level) process, whose process id was 1234.
If you are running a job and wish to do something else you may hit the
key ^Z (control-Z) which sends a STOP signal to the current job. The
shell will then normally indicate that the job has been `Stopped', and
print another prompt. You can then manipulate the state of this job,
putting it in the background with the bg command, or run some other com-
mands and then eventually bring the job back into the foreground with the
foreground command fg. A ^Z takes effect immediately and is like an in-
terrupt in that pending output and unread input are discarded when it is
typed. There is another special key ^Y which does not generate a STOP
signal until a program attempts to read(2) it. This can usefully be
typed ahead when you have prepared some commands for a job which you wish
to stop after it has read them.
A job being run in the background will stop if it tries to read from the
terminal. Background jobs are normally allowed to produce output, but
this can be disabled by giving the command ``stty tostop''. If you set
this tty option, then background jobs will stop when they try to produce
output like they do when they try to read input.
There are several ways to refer to jobs in the shell. The character `%'
introduces a job name. If you wish to refer to job number 1, you can
name it as `%1'. Just naming a job brings it to the foreground; thus
`%1' is a synonym for `fg %1', bringing job 1 back into the foreground.
Similarly saying `%1 &' resumes job 1 in the background. Jobs can also
be named by prefixes of the string typed in to start them, if these pre-
fixes are unambiguous, thus `%ex' would normally restart a suspended
ex(1) job, if there were only one suspended job whose name began with the
string `ex'. It is also possible to say `%?string' which specifies a job
whose text contains string, if there is only one such job.
The shell maintains a notion of the current and previous jobs. In output
pertaining to jobs, the current job is marked with a `+' and the previous
job with a `-'. The abbreviation `%+' refers to the current job and `%-'
refers to the previous job. For close analogy with the syntax of the
history mechanism (described below), `%%' is also a synonym for the cur-
rent job.
The job control mechanism requires that the stty(1) option new be set. It
is an artifact from a new implementation of the tty driver which allows
generation of interrupt characters from the keyboard to tell jobs to
stop. See stty(1) for details on setting options in the new tty driver.
Status reporting
This shell learns immediately whenever a process changes state. It nor-
mally informs you whenever a job becomes blocked so that no further
progress is possible, but only just before it prints a prompt. This is
done so that it does not otherwise disturb your work. If, however, you
set the shell variable notify, the shell will notify you immediately of
changes of status in background jobs. There is also a shell command
notify which marks a single process so that its status changes will be
immediately reported. By default notify marks the current process; sim-
ply say `notify' after starting a background job to mark it.
When you try to leave the shell while jobs are stopped, you will be
warned that `You have stopped jobs.' You may use the jobs command to see
what they are. If you do this or immediately try to exit again, the
shell will not warn you a second time, and the suspended jobs will be
terminated.
File Name Completion
When the file name completion feature is enabled by setting the shell
variable filec (see set), csh will interactively complete file names and
user names from unique prefixes, when they are input from the terminal
followed by the escape character (the escape key, or control-[) For exam-
ple, if the current directory looks like
DSC.OLD bin cmd lib xmpl.c
DSC.NEW chaosnet cmtest mail xmpl.o
bench class dev mbox xmpl.out
and the input is
% vi ch<escape>
csh will complete the prefix ``ch'' to the only matching file name
``chaosnet'', changing the input line to
% vi chaosnet
However, given
% vi D<escape>
csh will only expand the input to
% vi DSC.
and will sound the terminal bell to indicate that the expansion is incom-
plete, since there are two file names matching the prefix ``D''.
If a partial file name is followed by the end-of-file character (usually
control-D), then, instead of completing the name, csh will list all file
names matching the prefix. For example, the input
% vi D<control-D>
causes all files beginning with ``D'' to be listed:
DSC.NEW DSC.OLD
while the input line remains unchanged.
The same system of escape and end-of-file can also be used to expand par-
tial user names, if the word to be completed (or listed) begins with the
character ``~''. For example, typing
cd ~ro<escape>
may produce the expansion
cd ~root
The use of the terminal bell to signal errors or multiple matches can be
inhibited by setting the variable nobeep.
Normally, all files in the particular directory are candidates for name
completion. Files with certain suffixes can be excluded from considera-
tion by setting the variable fignore to the list of suffixes to be ig-
nored. Thus, if fignore is set by the command
% set fignore = (.o .out)
then typing
% vi x<escape>
would result in the completion to
% vi xmpl.c
ignoring the files "xmpl.o" and "xmpl.out". However, if the only comple-
tion possible requires not ignoring these suffixes, then they are not ig-
nored. In addition, fignore does not affect the listing of file names by
control-D. All files are listed regardless of their suffixes.
Substitutions
We now describe the various transformations the shell performs on the in-
put in the order in which they occur.
History substitutions
History substitutions place words from previous command input as portions
of new commands, making it easy to repeat commands, repeat arguments of a
previous command in the current command, or fix spelling mistakes in the
previous command with little typing and a high degree of confidence.
History substitutions begin with the character `!' and may begin anywhere
in the input stream (with the proviso that they do not nest.) This `!'
may be preceded by an `\' to prevent its special meaning; for conve-
nience, a `!' is passed unchanged when it is followed by a blank, tab,
newline, `=' or `('. (History substitutions also occur when an input
line begins with `^'. This special abbreviation will be described lat-
er.) Any input line which contains history substitution is echoed on the
terminal before it is executed as it could have been typed without histo-
ry substitution.
Commands input from the terminal which consist of one or more words are
saved on the history list. The history substitutions reintroduce se-
quences of words from these saved commands into the input stream. The
size of which is controlled by the history variable; the previous command
is always retained, regardless of its value. Commands are numbered se-
quentially from 1.
For definiteness, consider the following output from the history command:
9 write michael
10 ex write.c
11 cat oldwrite.c
12 diff *write.c
The commands are shown with their event numbers. It is not usually nec-
essary to use event numbers, but the current event number can be made
part of the prompt by placing an `!' in the prompt string.
With the current event 13 we can refer to previous events by event number
`!11', relatively as in `!-2' (referring to the same event), by a prefix
of a command word as in `!d' for event 12 or `!wri' for event 9, or by a
string contained in a word in the command as in `!?mic?' also referring
to event 9. These forms, without further modification, simply reintro-
duce the words of the specified events, each separated by a single blank.
As a special case `!!' refers to the previous command; thus `!!' alone
is essentially a redo.
To select words from an event we can follow the event specification by a
`:' and a designator for the desired