# C shell

C shell（csh）是[比尔·乔伊](https://www.edgepedia.org/zh/bill-joy)（Bill Joy）在加州大学伯克利分校（University of California, Berkeley）编写的一种 Unix 命令解释器，它把键入的命令行转换为系统动作，其特色在于模仿 C 编程语言的语法，以及当时 [Bourne shell](https://www.edgepedia.org/zh/bourne-shell) 所不具备的交互特性，包括命令历史机制、作业控制、别名，以及交互式的文件名与用户名补全。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup><sup> • </sup><sup>[2](https://www.bitsavers.org/pdf/stanford/stanford_4.2_BSD_manual/4.2_BSD_Vol_2C.pdf)</sup> 它于 1979 年 5 月随 2BSD 首次公开发行，同年晚些时候被纳入 3BSD。<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup>

| 关键事实 | 详情 |
|---|---|
| 作者与源起 | 比尔·乔伊，加州大学伯克利分校，作为 BSD Unix 项目的一部分；1978 年开发，1979 年 5 月随 2BSD 首次发行<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> |
| 标志性特性 | 历史机制、作业控制设施、交互式文件名与用户名补全，以及类 C 语法<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup> |
| 默认提示符 | `%`，而 sh、bash 和 ksh 中为 `$`<sup>[4](https://mirror.math.princeton.edu/pub/tldp/LDP/intro-linux/html/x12249.html)</sup> |
| 脚本能力 | 没有 shell 函数；命令行编辑、拼写纠正和提示符格式化直到 tcsh 才出现<sup>[5](https://subscription.packtpub.com/book/cloud-and-networking/9781788993197/1/ch01lvl1sec02/comparison-of-shells)</sup> |
| 增强的后继者 | tcsh，伯克利 C shell 的增强、完全兼容版本；当前版本 6.24.16，BSD-3-Clause 许可证<sup>[6](https://formulae.brew.sh/formula/tcsh)</sup> |
| 谱系 | 并非 Bourne shell 的后裔；csh 与 tcsh 不兼容 Bourne<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> |
| 曾经的默认角色 | Mac OS X 直至 10.2 的默认用户 shell（tcsh）；FreeBSD 直至 2023 年 14 版的默认 root shell<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> |

## 历史

C shell 由比尔·乔伊在加州大学伯克利分校编写，是[伯克利软件发行版](https://www.edgepedia.org/zh/berkeley-software-distribution)（Berkeley Software Distribution，BSD）Unix 的一部分，时间在第七版（Version 7）之后两年左右，它是第一个被广泛使用的 Bourne shell（sh）替代品。<sup>[7](https://web.eecs.utk.edu/~miturria/ksh/ch01_03.htm)</sup> 乔伊为 4.2BSD 手册撰写的导言中，把 csh 简单描述为“UNIX 上某一种特定命令解释器的名字”，其主要用途是把在终端上键入的命令行转换为系统动作，例如调用其他程序。<sup>[2](https://www.bitsavers.org/pdf/stanford/stanford_4.2_BSD_manual/4.2_BSD_Vol_2C.pdf)</sup> 其设计借鉴了该文档中致谢的几位贡献者：迈克尔·乌贝尔（Michael Ubell）实现了一个基于输入文本词结构的原型历史机制，迈克·奥布莱恩（Mike O'Brien）提出了加速命令执行的路径名散列机制，吉姆·库尔普（Jim Kulp）添加了作业控制与目录栈原语。<sup>[2](https://www.bitsavers.org/pdf/stanford/stanford_4.2_BSD_manual/4.2_BSD_Vol_2C.pdf)</sup>

多年来，Unix 社区习惯上把 Bourne shell 留给脚本编写，而日常交互会话则转向 C shell，因为 csh 提供了更适合交互工作的便利，如命令历史和作业控制。<sup>[8](https://www.oreilly.com/library/view/unix-in-a/0596100299/ch04.html)</sup> 许多年间，该 shell 只在运行 BSD 变体 Unix 的站点上可用，因此与 Bourne shell 不同，无法保证某个系统上一定存在 `csh` 命令。<sup>[9](https://www.rigacci.org/docs/biblio/online/sysadmin/ch12.htm)</sup>

## 交互特性

Debian 与 FreeBSD 的手册页指出了定义 csh 的四项能力：历史机制、作业控制设施、交互式文件名与用户名补全，以及类 C 语法；该 shell 既可作为交互式登录 shell，也可作为 shell 脚本的处理器。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup>

**历史替换。** 历史替换把先前命令输入中的词放入新命令，使得重复命令、复用参数或修正拼写错误都只需很少的键入。替换以字符 `!` 开头，可出现在输入流的任何位置；单独一个 `!!` 重复上一条命令，而事件指示符可以按编号（`!11`）、相对位置（`!-2`）、命令前缀（`!d`）或所含字符串（`!?mic?`）来选择。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup> 以 `^` 开头的行是替换上一行文本的简写，因此 `^lb^lib` 可修正刚键入命令中的一处拼写。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup> 词指示符如 `^`（第一个参数）、`$`（最后一个参数）和 `*`（所有参数），以及 `:h`（路径名的头部）和 `s/l/r/`（替换）之类的修饰符，作用于选定的词。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup>

**作业控制。** 该 shell 把每个管道关联为一个作业，并维护一张由 `jobs` 命令打印的当前作业表。Control-Z 以 SIGSTOP 信号挂起当前作业，之后 `bg` 让它在后台恢复，`fg` 把它带回前台；作业可以用 `%1`、无歧义的命令名前缀（如 `%ex`）或文本搜索（如 `%?string`）来引用。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup>

**文件名补全。** 当设置了 `filec` 变量时，若输入后跟转义字符，csh 便根据唯一前缀补全文件名和用户名。在含有唯一匹配项“chaosnet”的目录中键入 `vi ch<escape>` 会补全该词；而像 `D` 这样的歧义前缀只会展开到公共部分“DSC.”并响铃，改用 control-D 则会列出所有匹配名称，同时保持输入行不变。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup> `fignore` 变量可以把具有给定后缀的文件排除在补全候选之外。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup>

**别名与变量。** 该 shell 维护一张由 `alias` 和 `unalias` 建立的别名表；解析之后，每条命令的第一个词会与表对照，若命中则替换为别名文本，并在历史机制可用的情况下重新读取。别名可以引入解析器的元语法，如 `alias print 'pr \!* | lpr'`。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup> 变量保存零个或多个词的列表，`@` 命令执行数值计算并把结果赋给变量。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup>

## 语法及其与 Bourne 家族的区别

C shell 的语法借自 C 语言而非 Bourne shell。即便在细节上，两者的差异也一目了然：默认用户提示符是 `%` 而非 `$`，强制覆盖重定向用 `>!` 而非 `>|`，把标准输出和标准错误同时重定向到文件用 `>& file` 而非 `> file 2>&1`，命令替换只用反引号，没有 `$(command)` 形式。<sup>[4](https://mirror.math.princeton.edu/pub/tldp/LDP/intro-linux/html/x12249.html)</sup> 条件运算符 `||` 和 `&&` 分隔管道，其含义与 C 中一样：第二个管道分别只在第一个失败或成功时才执行。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup>

这两个谱系彼此独立，而非变体：csh 与 tcsh 的谱系不同于 Bourne shell，且不兼容 Bourne。<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> 与 Bourne、TC、Korn 和 bash 各 shell 的特性对比显示，csh 提供了 Bourne shell 所缺的别名、文件名补全、目录栈（`pushd` 与 `popd`）、历史和作业控制，却完全没有 shell 函数，也缺少命令行编辑、高级模式匹配、按键绑定、拼写纠正和提示符格式化，这些由 tcsh 补上。<sup>[5](https://subscription.packtpub.com/book/cloud-and-networking/9781788993197/1/ch01lvl1sec02/comparison-of-shells)</sup> C shell 除通配符之外，确实提供单键触发的命令与文件名补全。<sup>[10](https://linuxclass.heinz.cmu.edu/misc/shell-comparison.htm)</sup>

## tcsh

C shell 最重要的演化一步是 tcsh。1981 年 12 月，卡内基梅隆大学（Carnegie Mellon）的肯·格里尔（Ken Greer）把 TENEX 风格的文件名补全并入 csh，随后于 1983 年与仙童人工智能实验室（Fairchild A.I. Labs）的迈克·埃利斯（Mike Ellis）合作，首次公开发布了 tcsh 的源码。<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> tcsh 完全向后兼容 csh，因此任何有效的 csh 脚本都可在 tcsh 中不加修改地运行，反之则不然。<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup><sup> • </sup><sup>[4](https://mirror.math.princeton.edu/pub/tldp/LDP/intro-linux/html/x12249.html)</sup> 与普通 csh 相比，tcsh 增加了命令行编辑、按键绑定、拼写纠正和提示符格式化。<sup>[5](https://subscription.packtpub.com/book/cloud-and-networking/9781788993197/1/ch01lvl1sec02/comparison-of-shells)</sup> 该项目延续至今：Homebrew 软件包将其描述为“伯克利 C shell 的增强、完全兼容版本”，以 BSD-3-Clause 许可证发行 6.24.16 版，二进制文件名为 `csh` 和 `tcsh`。<sup>[6](https://formulae.brew.sh/formula/tcsh)</sup>

## 作为脚本语言的批评

C shell 在脚本编写方面的名声不佳，这一批评有明确可考的出处。汤姆·克里斯蒂安森（Tom Christiansen）于 1995 年发表《Csh 编程有害论》（"Csh Programming Considered Harmful"），1996 年将其张贴到 Usenet；他的核心论点是：尽管 csh 的类 C 条件语句对程序员颇具诱惑，但该 shell 不足以胜任非平凡的脚本编写，所有 shell 脚本都应使用 Bourne shell 或其后继者来写。<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> 该 FAQ 文本本身开篇即指出，“csh 具有诱惑力，因为其条件语句更像 C，于是人们选择阻力最小的路径，写起了 csh 脚本”，并指出在 Bourne shell 中可以打开或复制任意文件描述符。<sup>[11](https://www.cs.uleth.ca/~holzmann/C/shells/csh_avoidit.html)</sup> 手册页记录了一个相关的实际隐患：由于许多系统运行的是第 6 版或第 7 版 shell，其脚本与 csh 不兼容，因此若脚本的第一个字符不是井号，该 shell 会执行“标准”shell。<sup>[1](https://manpages.debian.org/testing/csh/csh.1.en.html)</sup> 克里斯蒂安森的批评之后形成的共识是：用于脚本编写时，POSIX sh、bash 或 zsh 远比 csh 更受推荐。<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> 一份当代的对比教程更直白地评价其交互的一面：C shell“其实并没有命令编辑功能”，其历史替换学起来有些复杂，用起来也可能笨拙，与 Korn、POSIX、Bourne Again、TC 或 Z 各 shell 的命令编辑相比相形见绌。<sup>[10](https://linuxclass.heinz.cmu.edu/misc/shell-comparison.htm)</sup>

## 默认地位的衰落

C shell 家族曾占据若干显要的默认 shell 位置，又在二十年间一一失去。从 Mac OS X 首次公开发行直至 10.2 版（Jaguar，2002 年），苹果把 tcsh 设为操作系统的默认 shell，让新一代用户接触到 csh 家族的语法；2003 年 Mac OS X 10.3（Panther）起，bash 取代它成为默认。<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> FreeBSD 一直把 csh（实际上是 tcsh）保留为默认 root shell，直到 2023 年 FreeBSD 14 将其改为 sh，一部 shell 史著作称这标志着 csh 作为系统默认 shell 的时代终结。<sup>[3](https://codearchaeology.dev/languages/c-shell/)</sup> 一份部署调查把 C shell 剩余的常见部署列为 BSD 系统与科学计算。<sup>[12](https://fsciences.univ-setif.dz/uploads/production_pedagogique/attachment/2372/Handout_USA_Final.pdf)</sup> 尽管如此，该 shell 仍拥有忠实的用户；一篇回顾指出，csh 及其改版 tcsh 直到大概 2000 年都还是最好的交互式 shell，而比尔·乔伊没有继续开发，因此原版 shell 停滞了。<sup>[13](https://softpanorama.org/People/Shell_giants/introduction.shtml)</sup>

## References

1. [csh(1) — Debian Manpages](https://manpages.debian.org/testing/csh/csh.1.en.html)
2. [William Joy, "An introduction to the C shell," 4.2BSD manual, bitsavers](https://www.bitsavers.org/pdf/stanford/stanford_4.2_BSD_manual/4.2_BSD_Vol_2C.pdf)
3. [C Shell — CodeArchaeology](https://codearchaeology.dev/languages/c-shell/)
4. [Differing features, Introduction to Linux (TLDP mirror)](https://mirror.math.princeton.edu/pub/tldp/LDP/intro-linux/html/x12249.html)
5. [Ganesh Sanjiv Naik, "Comparison of shells," Learning Linux Shell Scripting, Second Edition, Packt](https://subscription.packtpub.com/book/cloud-and-networking/9781788993197/1/ch01lvl1sec02/comparison-of-shells)
6. [tcsh — Homebrew Formulae](https://formulae.brew.sh/formula/tcsh)
7. [History of UNIX Shells, Learning the bash Shell (UTK mirror)](https://web.eecs.utk.edu/~miturria/ksh/ch01_03.htm)
8. [Unix in a Nutshell, 4th Edition, Chapter 4: The Bash and Korn Shells, O'Reilly](https://www.oreilly.com/library/view/unix-in-a/0596100299/ch04.html)
9. [Ch 12 — The C Shell, sysadmin reference (Rigacci)](https://www.rigacci.org/docs/biblio/online/sysadmin/ch12.htm)
10. [Which Shell Is Right for You? Shell Comparison, Heinz College CMU](https://linuxclass.heinz.cmu.edu/misc/shell-comparison.htm)
11. [Csh Programming Considered Harmful (unix-faq/shell/csh-whynot), C. Holzmann mirror](https://www.cs.uleth.ca/~holzmann/C/shells/csh_avoidit.html)
12. [UNIX Shell Comparison and Characteristics, University of Ferhat Abbas handout](https://fsciences.univ-setif.dz/uploads/production_pedagogique/attachment/2372/Handout_USA_Final.pdf)
13. [Introduction to the Unix shell history, Softpanorama](https://softpanorama.org/People/Shell_giants/introduction.shtml)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Operating system concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/c-shell

English original: https://www.edgepedia.org/c-shell
