Class 4 of 37 • Linux Shell Essentials \t\t\t\t

C Shell (csh) in Linux Programming

Introduction to C Shell (csh) & TCSH

 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.

Linux Shell Types Evolution Diagram

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:

  • Command History Mechanism (!): Re-executing prior commands with !! (repeat last), !n (run command number n), and !string (run last command beginning with string).
  • Command Aliases: Creating user-defined abbreviations using alias ll 'ls -la'.
  • C-Style Arithmetic & Expressions: Evaluating integer expressions natively: @ x = 10 + 5.
  • Directory Stack Navigation: Storing and cycling through directories via pushd, popd, and dirs.
  • Job Control: Suspending foreground jobs with Ctrl+Z and manipulating them via bg and fg.
  • Initialization Scripts: Using .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:

Major Scripting Flaws in C Shell:

  • Redirection Glitches: Csh cannot redirect standard error (stderr) independently of standard output. You can redirect both together (>& file), but redirecting only stderr requires cumbersome subshell acrobatics: ( command > out ) >& err.
  • Defective Quoting: Unlike Bourne-derived shells where double quotes preserve newlines and whitespace reliably, Csh performs word splitting inconsistently inside quoted variables.
  • Unreliable Signal Handling: onintr (the Csh signal trap) cannot catch signals reliably across child processes or during system calls.
  • Lack of Functions: Classic C Shell has no function definitions (e.g. 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:

  • Electronic Design Automation (EDA): Major semiconductor software suites from Synopsys, Cadence, and Mentor Graphics for VLSI chip layout were developed in the 1980s and maintain large legacy toolchains calibrated for tcsh environment setups.
  • FreeBSD Operating System: TCSH remains the default interactive shell for the root superuser in FreeBSD.
  • Scientific Research Clusters: Legacy High-Performance Computing (HPC) environments running module environments often provide modulecmd csh bindings.

6. Hands-on Lab Practice Drill

Lab Exercise: C-Shell vs Bash Script Translation

Compare the C Shell script on the left with its standard POSIX/Bash equivalent on the right:

C Shell Version (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

Bash Equivalent (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

Q1: How do you set and export an environment variable in C Shell?

Answer: In C Shell, use setenv VAR "value" without an equals sign (e.g., setenv EDITOR vim). In Bash, you use export VAR="value".

Q2: What is the history event designator '!!' in C Shell?

Answer: !! (double exclamation) re-executes the immediately preceding command. For instance, running sudo !! re-executes the previous failed command with administrative privileges.

Q3: Why can't a Bash script run directly inside C Shell?

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)

How do I switch to C shell from Bash in Linux?

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.

Can I write functions in C Shell?

No. C Shell does not support user-defined functions like Bash or Ksh. You must use external script files or aliases with arguments.

Next Step in Your Linux Learning Path:

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

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!