C Shell Introduction and Resources

In a computer operating system, a "shell" is an interface that allows the user to issue commands and access the system's services. Technically, a graphical interface (like the Windows Desktop) is a type of shell. However, when most people talk about a shell they mean a CLI — a "Command Line Interface." This is also known as a "terminal" or just "the command line."

You may be familiar with command line shells already. The Command Prompt in Windows and the Terminal app in Mac are examples of shells. Unix-like systems (Linux and Macs) typically have something call the Bash shell as the default CLI. (It is also available for Windows, but is not the default.)

Why use a command-line shell?

What can you do in a graphical user interface? Click on things. You can click on things to open them, start them, move them from one folder or directory to another. But can you rename thousands of files all at once? Can you search the text of every document for a particular pattern of numbers and words that make up an address? Find all the images that were taken with a particular camera during a certain period of time and then apply the same color-correcting filter to all of them?

Using a CLI makes it possible to quickly do both routine and highly specialized tasks quickly and efficiently — it lets you take almost complete control of your computer.

Moreover, shells allow you to write scripts, or small programs that can interact directly with the operating system, executing a series of commands, and automating tasks.

This page on Unix shells provides great information on shell scripting in general.

Shells vs Shell Scripting

It is important to realize the difference between a shell and shell scripting. A shell is an interactive environment — the command line terminal. Shell scripting is writing a program that will get interpreted by that command-line environment as a series of commands being typed in one after another. (Note however, that you can effectively write a shell script on the command-line.)

Many Unix users like the C Shell environment, and dislike the scripting language.

Csh History

In 1977, Stephen Bourne developed the Bourne shell for the Unix system, to replace the earlier Thompson shell. The following year, Bill Joy released the C shell.

The idea behind the C shell was to create a syntax that was more like the C programming language, which Linux was built on. But it had a number of problems in this regard. It did, however, catch on as a command-line shell because it was easier to use with features like command histories and directory stacks.

However, the Bourne shell dominated for administrative scripting. Throughout the 1980s, however, both Bourne and C shells were widely used. In 1989 the Bourne Again Shell (Bash) was introduced. This brought most of the interactive-use enhancements from csh into the Bourne environment. Since then, Bash has been the most widely used shell — combining the best of each of the earlier shells.

Using Csh and Tcsh

Today, just as Bash is normally used instead of the old Bourne shell, Tcsh is used instead of the C shell.

Accessing Csh

None of the three major Operating System families (Windows, Mac, and Linux) use csh as the default shell. Mac and (most distributions of) Linux use Bash (sh) and Windows ships with Windows Power Shell and the Command Prompt.

Mac OS X and most Linux distros include tcsh. You can enter a C shell terminal by typing csh or tcsh at the command line. (Mac calls its built-in tcsh interpreter csh. Most Linux distros use tcsh but include a symbolic link so that typing either name works the same.) If you do not have tcsh or csh already, you will need to install it.

The csh shell isn't really native to the way Windows works. You are probably better sticking with the above mentioned Power Shell. However, if you are determined to use csh on Windows, you can try out Cygwin or the Hamilton C shell.

Csh Script Files

If you want to execute csh files directly, make sure they are executable, and specify the csh interpreter by placing the following on the first line of the file:

#!/bin/csh

Syntax Examples

The csh syntax is based on C, which makes it more conventional than Bourne/Bash code.

Here is a simple Bourne shell script:

#!/bin/sh
let x=11
if [ $x -gt 10 ]
then
 echo "X is greater than 10."
fi

Notice that the comparison is being done by using -gt, which runs a separate process, instead of an in-laguage comparison operator. Also, the if statement is terminated by the reversed-if fi.

Here's the same thing in csh:

#!/bin/csh
set x=11
if ($x > 10) then
 echo "X is greater than 10."
endif

Csh Resources

There are a huge number of resources for learning the C Shell. We've collected the best ones.

Online Resources

Tutorials and Introductions

Reference

On the Other Hand

Don't miss Csh Programming Considered Harmful for an in-depth look at why many think csh is terrible and should never be used by anybody, ever, at any time. And if that doesn't convince you, you can read Top Ten Reasons Not to Use the C-Shell.

Books

There are not a lot of books specific to the C shell, but there are a couple of good, if old, ones.

  • Using csh & tcsh: this is a classic book on working with the C shell, and it is no less relevant today than it was when first published in 1995. Be sure to check out the online archive, where you can read sections of the book before buying it. Note that the author is strongly against using csh for scripting (he prefers Bash or Perl), so the book is really about using the C shell terminal, not about scripting.
  • The Unix C Shell Field Guide: another classic text, this one from 1986. Over three decades later, it remains indispensable for serious csh users.

Should I learn the C shell?

If you are a network or systems administrator in a Unix environment, you will almost certainly run into the C shell, so it is good to at least have some familiarity with it. Casual users and even most developers will likely have an easier time dealing with Bash or Perl or Python.