Lably
Skip to content

C shell

Unix shell

  • 25 min read
  • 4 chapters

Related on Lably

Through Unix shell, Variable (high-level programming language) and Control flow

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 csh and the latter is the improved tcsh.

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 compatible 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.

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.

Bourne shell

<span id="mwag"></span><span class="ch" id="mwaw">#!/bin/sh</span> <span class="k" id="mwbA">if</span><span class="w" id="mwbQ"> </span><span class="o" id="mwbg">[</span><span class="w" id="mwbw"> </span><span class="nv" id="mwcA">$days</span><span class="w" id="mwcQ"> </span>-gt<span class="w" id="mwcg"> </span><span class="m" id="mwcw">365</span><span class="w" id="mwdA"> </span><span class="o" id="mwdQ">]</span> <span class="k" id="mwdg">then</span> <span class="w" id="mwdw"> </span><span class="nb" id="mweA">echo</span><span class="w" id="mweQ"> </span>This<span class="w" id="mweg"> </span>is<span class="w" id="mwew"> </span>over<span class="w" id="mwfA"> </span>a<span class="w" id="mwfQ"> </span>year. <span class="k" id="mwfg">fi</span>

C shell

<span id="mwgQ"></span><span class="c" id="mwgg">#!/bin/csh</span> <span class="k" id="mwgw">if</span> <span class="o" id="mwhA">(</span> <span class="nv" id="mwhQ">$days</span> > 365 <span class="o" id="mwhg">)</span> <span class="k" id="mwhw">then</span> <span class="k" id="mwiA"> </span><span class="nb" id="mwiQ">echo </span>This is over a year. <span class="k" id="mwig">endif</span>

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.

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.

Bourne shell

<span id="mwoQ"></span><span class="ch" id="mwog">#!/bin/sh</span> <span class="nv" id="mwow">i</span><span class="o" id="mwpA">=</span><span class="m" id="mwpQ">2</span> <span class="nv" id="mwpg">j</span><span class="o" id="mwpw">=</span><span class="m" id="mwqA">1</span> <span class="k" id="mwqQ">while</span><span class="w" id="mwqg"> </span><span class="o" id="mwqw">[</span><span class="w" id="mwrA"> </span><span class="nv" id="mwrQ">$j</span><span class="w" id="mwrg"> </span>-le<span class="w" id="mwrw"> </span><span class="m" id="mwsA">10</span><span class="w" id="mwsQ"> </span><span class="o" id="mwsg">]</span> <span class="k" id="mwsw">do</span> <span class="w" id="mwtA"> </span><span class="nb" id="mwtQ">echo</span><span class="w" id="mwtg"> </span><span class="s1" id="mwtw">'2 **'</span><span class="w" id="mwuA"> </span><span class="nv" id="mwuQ">$j</span><span class="w" id="mwug"> </span><span class="o" id="mwuw">=</span><span class="w" id="mwvA"> </span><span class="nv" id="mwvQ">$i</span> <span class="w" id="mwvg"> </span><span class="nv" id="mwvw">i</span><span class="o" id="mwwA">=</span><span class="sb" id="mwwQ">`</span>expr<span class="w" id="mwwg"> </span><span class="nv" id="mwww">$i</span><span class="w" id="mwxA"> </span><span class="s1" id="mwxQ">'*'</span><span class="w" id="mwxg"> </span><span class="m" id="mwxw">2</span><span class="sb" id="mwyA">`</span> <span class="w" id="mwyQ"> </span><span class="nv" id="mwyg">j</span><span class="o" id="mwyw">=</span><span class="sb" id="mwzA">`</span>expr<span class="w" id="mwzQ"> </span><span class="nv" id="mwzg">$j</span><span class="w" id="mwzw"> </span>+<span class="w" id="mw0A"> </span><span class="m" id="mw0Q">1</span><span class="sb" id="mw0g">`</span> <span class="k" id="mw0w">done</span>

C shell

<span id="mw1g"></span><span class="c" id="mw1w">#!/bin/csh</span> <span class="nb" id="mw2A">set </span><span class="nv" id="mw2Q">i</span> <span class="o" id="mw2g">=</span> 2 <span class="nb" id="mw2w">set </span><span class="nv" id="mw3A">j</span> <span class="o" id="mw3Q">=</span> 1 <span class="k" id="mw3g">while</span> <span class="o" id="mw3w">(</span> <span class="nv" id="mw4A">$j</span> <<span class="o" id="mw4Q">=</span> 10 <span class="o" id="mw4g">)</span> <span class="nb" id="mw4w">echo</span> <span class="s1" id="mw5A">'2 **'</span> <span class="nv" id="mw5Q">$j</span> <span class="o" id="mw5g">=</span> <span class="nv" id="mw5w">$i</span> @ i *<span class="o" id="mw6A">=</span> 2 @ j++ <span class="k" id="mw6Q">end</span>

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 i=$((i * 2)) or : "$((i *= 2))".)

Finally, here is a third example, showing the differing styles for a switch statement.

Bourne shell

<span id="mw9w"></span><span class="ch" id="mw-A">#!/bin/sh</span> <span class="k" id="mw-Q">for</span><span class="w" id="mw-g"> </span>i<span class="w" id="mw-w"> </span><span class="k" id="mw_A">in</span><span class="w" id="mw_Q"> </span>d* <span class="k" id="mw_g">do</span> <span class="w" id="mw_w"> </span><span class="k" id="mwAQA">case</span><span class="w" id="mwAQE"> </span><span class="nv" id="mwAQI">$i</span><span class="w" id="mwAQM"> </span><span class="k" id="mwAQQ">in</span> <span class="w" id="mwAQU"> </span>d?<span class="o" id="mwAQY">)</span><span class="w" id="mwAQc"> </span><span class="nb" id="mwAQg">echo</span><span class="w" id="mwAQk"> </span><span class="nv" id="mwAQo">$i</span><span class="w" id="mwAQs"> </span>is<span class="w" id="mwAQw"> </span>short<span class="w" id="mwAQ0"> </span><span class="p" id="mwAQ4">;;</span> <span class="w" id="mwAQ8"> </span>*<span class="o" id="mwARA">)</span><span class="w" id="mwARE"> </span><span class="nb" id="mwARI">echo</span><span class="w" id="mwARM"> </span><span class="nv" id="mwARQ">$i</span><span class="w" id="mwARU"> </span>is<span class="w" id="mwARY"> </span>long<span class="w" id="mwARc"> </span><span class="p" id="mwARg">;;</span> <span class="w" id="mwARk"> </span><span class="k" id="mwARo">esac</span> <span class="k" id="mwARs">done</span>

C shell

<span id="mwAR4"></span><span class="c" id="mwAR8">#!/bin/csh</span> <span class="k" id="mwASA">foreach </span>i <span class="o" id="mwASE">(</span> d* <span class="o" id="mwASI">)</span> <span class="k" id="mwASM">switch</span> <span class="o" id="mwASQ">(</span> <span class="nv" id="mwASU">$i</span> <span class="o" id="mwASY">)</span> <span class="k" id="mwASc">case </span>d?: <span class="nb" id="mwASg">echo</span> <span class="nv" id="mwASk">$i</span> is short <span class="k" id="mwASo">breaksw</span> <span class="k" id="mwASs"> default</span>: <span class="nb" id="mwASw">echo</span> <span class="nv" id="mwAS0">$i</span> is long <span class="k" id="mwAS4">endsw</span> <span class="k" id="mwAS8">end</span>

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 ("!!") 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.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication