C shell

The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the Berkeley Software Distribution (BSD) which Joy first distributed 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 macOS 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 (including Ubuntu), 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, C shell 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 (also known as "sh"), the then-dominant shell written by Stephen Bourne at Bell Labs. This example illustrates the C shell's more conventional expression operators and syntax.
The Bourne 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 codice_1 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 (a separate statement, but often written joined on the same line with a semicolon) and run that nested block. Otherwise, it would run the else. Hard-linking the test program as both "codice_2" and "codice_3" 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. (Modern POSIX shell "does" have such a grammar: the statement could be written or .)
Finally, here is a third example, showing the differing styles for a switch statement.
In the sh script, "codice_4" 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, "codice_5", typed as a command and referred to as "bang, bang,"cause the immediately preceding command to run. Other short keystroke combinations, e.g., "codice_6" 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 "codice_7" 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 codice_8 (change directory) 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 codice_9. 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 (stopped) 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 (if it exists) without having to search and can be refreshed with the codice_10 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., codice_11, 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 (see globbing).
The C shell also introduced several notational conveniences (sometimes known as extended globbing), since copied by other Unix shells.
Multiple directory-level wildcards, e.g., "codice_21", are supported.
Since version 6.17.01, recursive wildcarding à la zsh (e.g. "codice_22" or "codice_23") is also supported with the codice_24 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.
Joining.
Commands can be joined on the same line.
Piping.
Commands can be connected using a pipe, which causes the output of one command to be fed into the input of the next. Both commands run concurrently.
Running concurrently means "in parallel". In a multi-core (multiple processor) system, the piped commands may literally be executing at the same time, otherwise the scheduler in the operating system time-slices between them.
Given a command, e.g., "codice_36", the shell creates a pipe, then starts both codice_37 and codice_38 with stdio for the two commands redirected so that codice_37 writes its stdout into the input of the pipe while codice_38 reads stdin from the output of the pipe. Pipes are implemented by the operating system with a certain amount of buffering so that codice_37 can write for a while before the pipe fills but once the pipe fills any new write will block inside the OS until codice_38 reads enough to unblock new writes. If codice_38 tries to read more data than is available, it will block until codice_37 has written more data or until the pipe closes, e.g., if codice_37 exits.
Variable substitution.
If a word contains a dollar sign, "codice_46", the following characters are taken as the name of a variable and the reference is replaced by the value of that variable. Various editing operators, typed as suffixes to the reference, allow pathname editing (e.g., "codice_47" to extract just the extension) and other operations.
Quoting and escaping.
Quoting mechanisms allow otherwise special characters, such as whitespace, wildcards, parentheses, and dollar signs, to be taken as literal text.
Command substitution.
Command substitution allows the output of one command to be used as arguments to another.
Background execution.
Normally, when the C shell starts a command, it waits for the command to finish before giving the user another prompt signaling that a new command can be typed.
Subshells.
A subshell is a separate child copy of the shell that inherits the current state but can then make changes, e.g., to the current directory, without affecting the parent.
Control structures.
The C shell provides control structures for both condition-testing and iteration. The condition-testing control structures are the if and switch statements. The iteration control structures are the while, foreach and repeat statements.
if statement.
There are two forms of the if statement. The short form is typed on a single line but can specify only a single command if the expression is true.
if ( expression ) command
The long form uses then, else and endif keywords to allow for blocks of commands to be nested inside the condition.
if ( expression1 ) then
 commands
else if ( expression2 ) then
 commands
else
 commands
endif
If the else and if keywords appear on the same line, csh chains, rather than nests them; the block is terminated with a single endif.
switch statement.
The switch statement compares a string against a list of patterns, which may contain wildcard characters. If nothing matches, the default action, if there is one, is taken.
switch ( string )
 case pattern1:
 commands
 breaksw
 case pattern2:
 commands
 breaksw
 default:
 commands
 breaksw
endsw
while statement.
The while statement evaluates an expression. If it is true, the shell runs the nested commands and then repeats for as long as the expression remains true.
while ( expression )
 commands
end
foreach statement.
The foreach statement takes a list of values, usually a list of filenames produced by wildcarding, and then for each, sets the loop variable to that value and runs the nested commands.
foreach loop-variable ( list-of-values )
 commands
end
repeat statement.
The repeat statement repeats a single command an integral number of times.
repeat integer command
Variables.
The C shell implements both shell and environment variables. Environment variables, created using the codice_58 statement, are always simple strings, passed to any child processes, which retrieve these variables via the codice_59 argument to codice_60.
Shell variables, created using the codice_61 or codice_62 statements, are internal to C shell. They are not passed to child processes. Shell variables can be either simple strings or arrays of strings. Some of the shell variables are predefined and used to control various internal C shell options, e.g., what should happen if a wildcard fails to match anything.
In current versions of csh, strings can be of arbitrary length, well into millions of characters.
Expressions.
The C shell implements a 32-bit integer expression grammar with operators borrowed from C but with a few additional operators for string comparisons and filesystem tests, e.g., testing for the existence of a file. Operators must be separated by whitespace from their operands. Variables are referenced as codice_46"name".
Operator precedence is also borrowed from C, but with different operator associativity rules to resolve the ambiguity of what comes first in a sequence of equal precedence operators. In C, the associativity is left-to-right for most operators; in C shell, it is right-to-left. For example,
The parentheses in the C shell example are to avoid having the bit-shifting operators confused as I/O redirection operators. In either language, parentheses can always be used to explicitly specify the desired order of evaluation, even if only for clarity.
Reception.
Although Stephen Bourne himself acknowledged that csh was superior to his shell for interactive use, it has never been as popular for scripting. Initially, and through the 1980s, csh could not be guaranteed to be present on all Unix systems, but sh could, which made it a better choice for any scripts that might have to run on other machines. By the mid-1990s, csh was widely available, but the use of csh for scripting faced new criticism by the POSIX committee, which specified that there should only be one preferred shell, the KornShell, for both interactive and scripting purposes. The C shell also faced criticism from others over the C shell's alleged defects in syntax, missing features, and poor implementation.
It worked for most interactively typed commands, but for the more complex commands a user might write in a script, it could easily fail, producing only a cryptic error message or an unwelcome result. For example, the C shell could not support piping between control structures. Attempting to pipe the output of a codice_78 command into codice_79 simply didn't work. (The work-around, which works for many of the complaints related to the parser, is to break the code up into separate scripts. If the codice_78 is moved to a separate script, piping works because scripts are run by forking a new copy of csh that does inherit the correct stdio handles.)
Another example is the unwelcome behavior in the following fragments. Both of these appear to mean, "If 'myfile' does not exist, create it by writing 'mytext' into it." But the version on the right always creates an empty file because the C shell's order of evaluation is to look for and evaluate I/O redirection operators on each command line as it reads it, before examining the rest of the line to see whether it contains a control structure.
The implementation is also criticized for its notoriously poor error messages, e.g., "0 event not found", which yields no useful information about the problem.
Influence.
The C shell was extremely successful in introducing a large number of innovations including the history mechanism, aliases, tilde notation, interactive filename completion, an expression grammar built into the shell, and more, that have since been copied by other Unix shells. But in contrast to sh, which has spawned a large number of independently developed clones, including ksh and bash, only two csh clones are known. (Since tcsh was based on the csh code originally written by Bill Joy, it is not considered a clone.)
In 1986, Allen Holub wrote "On Command: Writing a Unix-Like Shell for MS-DOS", a book describing a program he had written called "SH" but which in fact copied the language design and features of csh, not sh. Companion diskettes containing full source for SH and for a basic set of Unix-like utilities (cat, cp, grep, etc.) were available for $25 and $30, respectively, from the publisher. The control structures, expression grammar, history mechanism and other features in Holub's SH were identical to those of the C shell.
In 1988, Hamilton Laboratories began shipping Hamilton C shell for OS/2. It included both a csh clone and a set of Unix-like utilities. In 1992, Hamilton C shell was released for Windows NT. The Windows version continues to be actively supported but the OS/2 version was discontinued in 2003. An early 1990 quick reference described the intent as "full compliance with the entire C shell language (except job control)" but with improvements to the language design and adaptation to the differences between Unix and a PC. The most important improvement was a top-down parser that allowed control structures to be nested or piped, something the original C shell could not support, given its ad hoc parser. Hamilton also added new language features including built-in and user-defined procedures, block-structured local variables and floating point arithmetic. Adaptation to a PC included support for the filename and other conventions on a PC and the use of threads instead of forks (which were not available under either OS/2 or Windows) to achieve parallelism, e.g., in setting up a pipeline.