The C shell is a Unix shell that was created by Bill Joy while he was a
graduate student at University of California, Berkeley in the late 1970s.
It has been distributed widely, beginning with the 2BSD release of the
BSD Unix system that Joy began distributing in 1978. Other early
contributors to the ideas or the code were Michael Ubell, Eric Allman, Mike
O'Brien and Jim Kulp. The C shell is a command processor
typically run in a text window, allowing the user to type commands. The C shell
can also read commands from a file, called a script. Like all Unix shells,
it supports filename wildcarding, piping, here documents, command
substitution, variables and control structures for condition-testing and
iteration. What differentiated the C shell from others, especially in the
1980s, were its interactive features and overall style. Its new features made it
easier and faster to use. The overall style of the language looked more like C
and was seen as more readable. On many systems, such as Mac OS X and
Red Hat Linux, csh is actually tcsh, an improved version of csh. Often one of
the two files is either a hard link or a symbolic link to the other, so that
either name refers to the same improved version of the C shell.
On Debian and some derivatives, there are two different packages: csh and
tcsh. The former is based on the original BSD version of csh and the
latter is the improved tcsh. tcsh added filename and command
completion and command line editing concepts borrowed from the Tenex system,
which is the source of the "t". Because it only added functionality and did not
change what was there, tcsh remained backward compatible with the original C
shell. Though it started as a side branch from the original source tree Joy
had created, tcsh is now the main branch for ongoing development. tcsh is very
stable but new releases continue to appear roughly once a year, consisting
mostly of minor bug fixes. Design objectives and features
The main design objectives for the C shell were that it should look more like
the C programming language and that it should be better for interactive use.
= More like C = The Unix system had been written almost
exclusively in C, so the C shell's first objective was a command language that
was more stylistically consistent with the rest of the system. The keywords,
the use of parentheses and the C shell's built-in expression grammar and support
for arrays were all strongly influenced by C.
By today's standards, csh may not seem particularly more C-like than many other
popular scripting languages. But through the 80s and 90s, the difference was seen
as striking, particularly when compared to Bourne shell, the then-dominant shell
written by Stephen Bourne at Bell Labs. This example illustrates the C shell's
more conventional expression operators and syntax.
sh lacked an expression grammar. The square bracketed condition had to be
evaluated by the slower means of running the external test program. sh's if
command took its argument words as a new command to be run as a child process. If
the child exited with a zero return code, sh would look for a then clause
and run that nested block. Otherwise it would run the else. Hard-linking the
test program as both "test" and "[" gave the notational advantage of the square
brackets and the appearance that the functionality of test was part of the sh
language. sh's use of a reversed keyword to mark the end of a control block was a
style borrowed from ALGOL 68. By contrast, csh could evaluate the
expression directly, which made it faster. It also claimed better
readability: Its expressions used a grammar and a set of operators mostly
copied from C, none of its keywords were reversed and the overall style was also
more like C. Here is a second example, comparing
scripts that calculate the first 10 powers of 2.
Again because of the lack of an expression grammar, the sh script uses
command substitution and the expr command. The @ statement in C shell is a
pun: it's the "at-sign-ment" statement. Finally, here is a third example,
showing the differing styles for a switch statement.
In the sh script, ";;" marks the end of each case because sh disallows null
statements otherwise. = Improvements for interactive use =
The second objective was that the C shell should be better for interactive
use. It introduced numerous new features that made it easier, faster and more
friendly to use by typing commands at a terminal. Users could get things done
with a lot fewer keystrokes and it ran faster. The most significant of these
new features were the history and editing mechanisms, aliases, directory
stacks, tilde notation, cdpath, job control and path hashing. These new
features proved very popular, and many of them have since been copied by other
Unix shells. History
History allows users to recall previous commands and rerun them by typing only a
few quick keystrokes. For example, two exclamation marks, "!!", typed as a
command and referred to as "bang, bang," causes the immediately preceding command
to run. Other short keystroke combinations, e.g., "!$" to mean just
the last argument of the previous command, allow bits and pieces of
previous commands to be pasted together and edited to form a new command.
Editing operators Editing can be done not only on the text
of a previous command, but also on variable substitutions. Operators range
from simple string search/replace to parsing a pathname to extract a specific
segment. Aliases
Aliases allow the user to type the name of an alias and have the C shell expand
it internally into whatever set of words the user has defined. For many simple
situations, aliases run faster and are more convenient than scripts.
Directory stack The directory stack allows the user to
push or pop the current working directory, making it easier to jump back
and forth between different places in the filesystem.
Tilde notation Tilde notation offers a shorthand way of
specifying pathnames relative to the home directory using the "~" character.
Filename completion The escape key can be used interactively
to show possible completions of a filename at the end of the current
command line. Cdpath
Cdpath extends the notion of a search path to the cd command: If the specified
directory isn't in the current directory, csh will try to find it in
the cdpath directories. Job control
Well into the 1980s, most users only had simple character-mode terminals that
precluded multiple windows, so they could only work on one task at a time.
The C shell's job control allowed the user to suspend the current activity and
create a new instance of the C shell, called a job, by typing ^Z. The user
could then switch back and forth between jobs using the fg command. The active
job was said to be in the foreground. Other jobs were said to be either
suspended or running in the background. Path hashing
Path hashing speeds up the C shell's search for executable files. Rather than
performing a filesystem call in each path directory, one at a time, until it
either finds the file or runs out of possibilities, the C shell consults an
internal hash table built by scanning the path directories. That table can
usually tell the C shell where to find the file without having to search, and
can be refreshed with the rehash command.
Overview of the language The C shell operates one line at a time.
Each line is tokenized into a set of words separated by spaces or other
characters with special meaning, including parentheses, piping and
input/output redirection operators, semicolons, and ampersands.
= Basic statements = A basic statement is one that simply
runs a command. The first word is taken as name of the command to be run and may
be either an internal command, e.g., echo, or an external command. The rest
of the words are passed as arguments to the command.
At the basic statement level, here are some of the features of the grammar:
Wildcarding The C shell, like all Unix shells,
treats any command-line argument that contains wildcard characters as a
pattern and replaces it with the list of all the filenames that match.
* matches any number of characters. ? matches any single character.
[...] matches any of the characters inside the square brackets. Ranges are
allowed, using the hyphen. [!...] matches any character not in the
set. The C shell also introduced several
notational conveniences, since copied by other Unix shells.
abc{def,ghi} is alternation and expands to abcdef abcghi.
~ means the current user's home directory.
~user means user's home directory. Multiple directory-level wildcards,
e.g., "*/*.c", are supported. Since version 6.17.01, recursive
wildcarding à la zsh is also supported with the globstar option.
Giving the shell the responsibility for interpreting wildcards was an important
decision on Unix. It meant that wildcards would work with every command,
and always in the same way. However, the decision relied on Unix's ability to
pass long argument lists efficiently through the exec system call that csh
uses to execute commands. By contrast, on Windows, wildcard interpretation is
conventionally performed by each application. This is a legacy of MS-DOS,
which only allowed a 128-byte command line to be passed to an application,
making wildcarding by the DOS command prompt impractical. Although modern
Windows can pass command lines of up to roughly 32K Unicode characters, the
burden for wildcard interpretation remains with the application.
I/O redirection By default, when csh runs a command, the
command inherits the csh's stdio file handles for stdin, stdout and stderr,
which normally all point to the console window where the C shell is running. The
i/o redirection operators allow the command to use a file instead for input
or output. > file means stdout will be written to
file, overwriting it if it exists, and creating it if it doesn't. Errors still
come to the shell window. >& file means both stdout and stderr
will be written to file, overwriting it if it exists, and creating it if it
doesn't. >> file means stdout will be appended at
the end of file. >>& file means both stdout and stderr
will be appended at the end of file.