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.Harley Hahn, Harley Hahn's Guide to Unix and Linux .Berkeley Engineering Lab Notes, Volume 1, Issue 2, October 2001 . Other early contributors to the ideas or the code were Michael Ubell, Eric Allman, Mike O'Brien and Jim Kulp.An Introduction to the C shell by Bill Joy.
The C shell is a command processor which is typically run in a text window, allowing the user to type and execute 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 replaced by 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. The original csh source code, under src/bin/csh/, and binary are part of NetBSD, and OpenBSD.
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 cshUbuntu - Details of package csh. launchpad.net.Debian - Details of package csh. tracker.debian.org. and the latter is the improved tcsh.Ubuntu - Details of package tcsh. launchpad.net.Debian - Details of package tcsh. tracker.debian.org.
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 already existed, tcsh remained backward compatibletcsh(1) man page. tcsh. 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.Fixes file in tcsh-17 June 2000.
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 1980s 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 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 (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 "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.Re: Late Bloomers Revisited USENET post to comp.lang.misc by Piercarlo "Peter" Grandi, Dept of CS, UCW Aberystwyth, UK, 17 December 1989.
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, ";;" 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, typing two exclamation marks ("!!")Pronounced "bang, bang" as a command causes the immediately preceding command to be run. Other short keystroke combinations, e.g., "!$" (meaning "the final 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 (change directory) command: If the specified directory is not 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 (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 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 (see globbing).
* 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 (sometimes known as extended globbing), since copied by other Unix shells.
abc{def,ghi} is alternation (aka brace expansion) 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 in zsh (e.g. "**/*.c" or "***/*.html") 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. < file means stdin will be read from file. << string is a here document. Stdin will read the following lines up to the one that matches string.
Redirecting stderr alone isn't possible without the aid of a sub-shell.
set filter = "$home"'/filter' mkfifo "$filter" cat "$filter" & ( ( ls /root/ || echo No access. ) > "$filter" ) >& /dev/null
Systems supporting file descriptors as files may use the following workaround.
( ( ( echo ok ; '' ) > /dev/fd/0 ) >& /dev/null < /dev/fd/1 ) | ( echo "$<" bye )
Joining
Commands can be joined on the same line.
; means run the first command and then the next. && means run the first command and, if it succeeds with a 0 return code, run the next.