C shell
Original author(s) | Bill Joy |
---|---|
Initial release | 1979 |
Repository | |
Operating system | BSD, various |
Available in | C |
Type | Unix Shell |
License | BSD license |
The C shell (csh) is a Unix shell developed by Bill Joy for the BSD Unix system. The scripting syntax is modeled after the C programming language. Earlier releases of OpenBSD provided the C shell as the default shell on the system.[1] The C shell was derived from the 6th Edition Unix Thompson shell which also was the predecessor of the Bourne shell. The C shell added many feature improvements over the Bourne shell, such as aliases and command history.
Today, the original C shell is not in wide use on Unix. The TENEX C Shell (tcsh), which is backward compatible with C shell, has become the default shell for FreeBSD. This improved C shell is often mapped to the "csh" executable so to seamlessly replace it. Improvements include filename completion and command line editing.
Features
The C shell has the typical Unix shell structure. Each line of input is interpreted as a separate command to execute. Backslash is the "escape character" and can be used to continue commands on to the next line.
New features
These features were first introduced in C shell, though most are now common to many Unix shells.
- Job control
- The ability to suspend an interactive job and resume it at a later time, or send it into the "background".
- History substitution
- The
!
-substitution feature allowed previous commands to be re-executed or edited in a variety of ways. - Arrays
- Variables can contain multiple elements, which can be numerically indexed.
~
expansion- Home directory by username lookup, using
~
was popular enough that most modern, Unix-based Web servers now support it [2], in addition to most shells. - Aliases
- A command could be aliased to another command or command sequence.
- Math operations
- Provided arithmetical operations which sh does not provide except by calling external programs.
Syntax differences
The most obvious difference between C shell and Bourne shell syntax was how their built-in conditional operations called out to test--the C shell used parentheses, and the Bourne shell used square brackets. Later versions of Bourne shell would rectify this by using a built-in test operator.
Variable assignment was also performed slightly differently with "set a = b
" rather than Bourne shell's "a=b
".
Criticism
While the C shell introduced many innovative features in interactive mode, the C shell's scripting capabilities came under criticism in the early-to-mid 1990s for their ambiguity, especially with respect to quoting and redirection. Combined with the fact that Bourne shell is guaranteed to be present on all Single UNIX Specification-compliant systems, sh is usually used for scripting purposes over C shell on Unix.
An example of possibly unwelcome behavior of csh scripts is the following:
if ( ! -e myfile ) echo mytext > myfile if ( ! -e myfile ) then echo mytext > myfile endif
Both of these would appear to mean "if file 'myfile' does not exist, create it with contents 'mytext'". But the first example will in fact create an empty file, as the line is parsed such that the file existence is not tested until after the output redirect has been set up.
If scripts are written in csh, it is often useful to run the script with the -f flag, (e.g. by starting with the line #!/bin/csh -f) in order to avoid running the user's setup file as this may affect the script's behavior.
Another known problem of csh is its message "0 event not found", which yields no information about what the problem is.
See also
- tcsh, a Unix shell based on and compatible with the C shell (csh)
- Comparison of computer shells
- test (Unix)
Further reading
- Anderson, Gail (1986). The UNIX C Shell Field Guide. Prentice-Hall. ISBN 0-13-937468-X.
{{cite book}}
: Unknown parameter|coauthors=
ignored (|author=
suggested) (help) - Wang, Paul (1988). An Introduction to Berkeley UNIX. Wadsworth Pub. Co. ISBN 0-534-08862-7.
- DuBois, Paul (1995). Using csh & tcsh. O'Reilly & Associates. ISBN 1-56592-132-1.
- Arick, Martin R. (1993). UNIX C Shell Desk Reference. John Wiley & Sons. ISBN 0471556807.
References
External links
- An Introduction to the C shell by William Joy
- Csh Programming Considered Harmful by Tom Christiansen
- Top 10 Reasons not to use the C shell by Bruce Barnett
- cshell options and syntax