What is csh?
Author: Domeny.ForSale editorial team Published
In brief: csh, or C shell, is a system shell: a program that interprets the commands you enter and runs other programs on Unix and related systems. It supports interactive work and script execution, and its syntax partly resembles the C language. You may encounter it in older environments, technical tools and existing scripts; for a new administration script, Bash or sh will usually be more practical.
What is csh used for, and how does it differ from a terminal?
A terminal is a text interface where you enter commands and read their output. A shell runs within this environment: it reads commands, expands shorthand expressions, handles redirections and launches programs. Simply opening a terminal window therefore does not mean you are using csh — it may be running Bash, Zsh or another shell.
C shell was developed at the University of California, Berkeley, and its principal creator is Bill Joy. Its name refers to the resemblance of some control structures to the C language, not to an ability to execute C code. You cannot run a C source file through csh as you would an ordinary script.
With csh, you can move between directories, run tools, connect them through pipes and direct output to files. The shell also supports command history, aliases — your own command shortcuts — and management of jobs running in the background.
Many simple commands look the same across different shells. For example, ls runs a program that lists the contents of a directory. Differences become important when dealing with variables, conditionals, loops and error-message redirection.
What does csh syntax look like?
In csh, you set an ordinary variable with set and an environment variable with setenv. An environment variable is passed to programs you launch, allowing it to influence their behaviour.
set name = "Anna"
echo "Hello, $name"
setenv APP_MODE test
The first variable is used here to construct a message. The second makes the value APP_MODE=test available to programs you launch; its meaning depends on whether a particular program reads it.
An example csh loop looks like this:
foreach colour (red green blue)
echo "Colour: $colour"
end
This loop will print three lines. It will not work unchanged in Bash, because Bash uses a different loop structure. Likewise, an instruction found in a Bash tutorial, such as export APP_MODE=test, is not the correct way to set an environment variable in csh.
Do not move entire script fragments between shells without checking the syntax. Compatibility of a single command, such as echo, does not mean the entire script is compatible.
How can you check whether csh is on your system?
On a Unix system, you can check whether the program is available through the current command search path:
which csh
If you receive a path, such as /bin/csh, the system can locate a command under that name. No output does not always prove that it is not installed: the program may be outside the directories searched by the shell. The availability of the which tool and the format of its messages also depend on the system.
The command echo $SHELL usually shows the path to the login shell, meaning the default shell assigned to the user. It is not a reliable test of the shell currently running, because the value of this variable may remain unchanged after you manually launch another shell.
If csh is available, entering csh will start an additional shell session. The exit command will return you to the previous one. This is a simpler way to try it out than changing your account’s default shell; for installation instructions, it is best to consult the documentation for your particular system.
Csh, tcsh, Bash and sh — which should you choose?
tcsh is an extension of csh, broadly compatible with it and offering enhanced interactive features, including command-line editing and name completion. On some systems, the csh command actually launches tcsh. When diagnosing problems, the actual implementation therefore matters, not just the command name.
| Shell | When it makes sense |
|---|---|
| csh | Maintaining scripts and environments that require C shell syntax. |
| tcsh | Interactive work in an environment based on the csh family. |
| Bash | New scripts when Bash is available on the target machines. |
| sh | Portable scripts using POSIX-compliant syntax. |
POSIX is a family of standards describing common interfaces for Unix systems, including shell behaviour. Csh does not conform to POSIX shell syntax. The recommendation to use Bash or sh for new scripts stems primarily from portability and the ease of using common administration tools and examples, not from an inability of csh to automate tasks.
How do you run a file intended for csh?
A file with the .csh extension usually contains a C shell script, but the extension alone does not determine its contents. The first line, such as #!/bin/csh, is a clue. This is a shebang: a line specifying the interpreter used when an executable file is run directly.
After reviewing the contents of a trusted file, you can run it explicitly:
csh script.csh
This method does not require you to grant the file execute permission, but it does require read permission. The script runs with the privileges of the user who launches it, so do not test an unknown file using sudo.
If a project’s instructions require csh, first check its availability and the project’s requirements. Do not automatically replace it with bash script.csh — changing the interpreter does not translate the script’s syntax.
See also: What happens after a domain expires?
