The C shell (csh) is a Unix command processor and scripting language created in 1978 by Bill Joy (co-founder of Sun Microsystems and creator of the vi editor) while he was a graduate student at the University of California, Berkeley. It was originally bundled with the 2BSD Unix release.
The C shell was designed specifically to resemble the syntax of the C programming language. What revolutionized Unix interactive computing in the 1980s was C Shell's focus on user convenience: it introduced command history recall, command aliases, filename completion, and job control—features that were completely absent from Stephen Bourne's early shell.
On modern Linux and BSD systems, /bin/csh is typically a symbolic link or direct compile of tcsh (TC Shell), an enhanced version developed by Ken Greer at Carnegie Mellon University that adds interactive command-line editing and programmable tab completion.
Figure 1: Position of C Shell (csh / tcsh) in the UNIX/Linux Shell family tree
1. Breakthrough Features Introduced by C Shell
Many interactive conventions that modern developers take for granted in Bash and Zsh originated in Bill Joy's C Shell:
!): Re-executing prior commands with !! (repeat last), !n (run command number n), and !string (run last command beginning with string).alias ll 'ls -la'.@ x = 10 + 5.pushd, popd, and dirs.Ctrl+Z and manipulating them via bg and fg..cshrc (run on every subshell) and .login (run at initial login).2. C Shell Script Syntax & Working Example
Notice the C-like syntax: expressions use parentheses ( ... ), blocks close with endif, and variables are assigned with set:
#!/bin/csh
# =========================================================
# Script: check_days.csh
# Target Interpreter: C Shell (/bin/csh or /bin/tcsh)
# =========================================================
# Variable assignment using 'set' (spaces around = are valid)
set days = 400
# C-style conditional comparison with parentheses
if ( $days > 365 ) then
echo "This is over a year."
else
echo "This is less than or equal to a year."
endif
# Iterating over lists using foreach
foreach fruit ( Apple Banana Orange )
echo "Fruit name: $fruit"
end
Output from executing under tcsh or csh:
$ chmod +x check_days.csh
$ ./check_days.csh
This is over a year.
Fruit name: Apple
Fruit name: Banana
Fruit name: Orange
3. Technical Comparison: C Shell (csh) vs Bash
| Feature | C Shell (csh / tcsh) |
Bourne-Again Shell (bash) |
|---|---|---|
| Variable Assignment | set var = "value" |
var="value" (no spaces around =) |
| Environment Variable | setenv PATH /usr/bin |
export PATH=/usr/bin |
| Arithmetic Evaluation | @ count = $count + 1 |
(( count++ )) or count=$((count+1)) |
| Conditional Syntax | if ( $val > 10 ) then ... endif |
if [ "$val" -gt 10 ]; then ... fi |
| Loop Construct | foreach item ( a b c ) ... end |
for item in a b c; do ... done |
| POSIX Compatibility | Non-compliant (completely different grammar) | POSIX compliant with extended features |
4. Why C Shell Scripting is Considered Harmful
In 1995, famed Perl architect Tom Christiansen published a canonical critique titled "Csh Programming Considered Harmful", detailing why system administrators and programmers should never write automation scripts in C Shell:
>& file), but redirecting only stderr requires cumbersome subshell acrobatics: ( command > out ) >& err.onintr (the Csh signal trap) cannot catch signals reliably across child processes or during system calls.my_func() { ... }), forcing scripts to use labels and goto statements.Golden Rule: Use C Shell or TCSH interactively if desired, but always write production shell scripts in POSIX /bin/sh or /bin/bash.
5. Where is C Shell Still Used Today?
Despite its scripting drawbacks, C Shell and TCSH maintain enduring presence in specialized technical fields:
tcsh environment setups.root superuser in FreeBSD.modulecmd csh bindings.6. Hands-on Lab Practice Drill
Compare the C Shell script on the left with its standard POSIX/Bash equivalent on the right:
sample.csh)#!/bin/csh
set score = 85
if ( $score >= 75 ) then
echo "Distinction Passed"
else
echo "Standard Pass"
endif
@ i = 1
while ( $i <= 3 )
echo "Counter: $i"
@ i++
end
sample.sh)#!/bin/bash
score=85
if [ "$score" -ge 75 ]; then
echo "Distinction Passed"
else
echo "Standard Pass"
fi
i=1
while [ "$i" -le 3 ]; do
echo "Counter: $i"
(( i++ ))
done
7. University Viva & Job Interview Q&A
Answer: In C Shell, use setenv VAR "value" without an equals sign (e.g., setenv EDITOR vim). In Bash, you use export VAR="value".
Answer: !! (double exclamation) re-executes the immediately preceding command. For instance, running sudo !! re-executes the previous failed command with administrative privileges.
Answer: C Shell has an entirely incompatible syntax grammar: it does not recognize fi, done, elif, or export, and uses set, endif, and end instead. Scripts must specify the proper interpreter via a shebang (#!/bin/bash).
8. Frequently Asked Questions (FAQ)
Install tcsh using your package manager (e.g. sudo apt install tcsh) and run tcsh or csh to enter the shell. Type exit to return to Bash.
No. C Shell does not support user-defined functions like Bash or Ksh. You must use external script files or aliases with arguments.
Learn how David Korn merged Bourne scripting with C shell features in Class 5: Korn Shell & ksh93.
Continue to Class 5: Korn Shell (ksh) → Previous: ← Bourne Shell | Full Syllabus