# 外壳（计算）

外壳是一种命令语言解释器，即一种读取用户键入的或存储在脚本文件中的命令并加以执行的程序，它同时也是一种编程语言，用于将操作系统的各种实用工具组合成新的命令。<sup>[1](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html)</sup> 这一名称源于外壳作为操作系统内核之外一个层面的位置；该术语似乎最早出现在MULTICS系统中。<sup>[2](https://www.nokia.com/bell-labs/publications-and-media/publications/unix-time-sharing-system-the-unix-shell/)</sup> S. R. 伯恩（S. R. Bourne）在1978年贝尔实验室的论文中将UNIX外壳描述为既是一种命令语言也是一种编程语言，包含控制流原语、变量和参数传递，并具有while、if、for和case等构造。<sup>[2](https://www.nokia.com/bell-labs/publications-and-media/publications/unix-time-sharing-system-the-unix-shell/)</sup>

| 关键事实 | 详情 |
|---|---|
| 第一个Unix外壳 | 肯·汤普森（Ken Thompson）的sh，随1971年的第一版Unix一同发布；是一个纯命令解释器，不具备脚本编写能力<sup>[3](https://myunix.org/guides/unix-shell-history/)</sup> |
| 伯恩外壳 | 由斯蒂芬·伯恩（Stephen Bourne）在贝尔实验室随UNIX第七版于1979年推出；是现代sh的直接祖先<sup>[4](https://web.eecs.utk.edu/~miturria/ksh/ch01_03.htm)</sup><sup> • </sup><sup>[5](https://www.gnu.org/s/bash/manual/bash.html)</sup> |
| 科恩外壳 | 由AT&T贝尔实验室的大卫·科恩（David Korn）编写；几乎向上兼容伯恩外壳，同时增加了历史记录、别名和作业控制<sup>[4](https://web.eecs.utk.edu/~miturria/ksh/ch01_03.htm)</sup> |
| 标准化 | POSIX.2（IEEE Std 1003.2-1992）定义了外壳命令语言，大量借鉴了ksh的行为<sup>[6](https://danielcosenza.com/posts/sh-history/)</sup> |
| Bash | GNU操作系统的默认外壳，是Bourne-Again SHell的缩写，大体上兼容sh，并融合了ksh和csh的特性<sup>[5](https://www.gnu.org/s/bash/manual/bash.html)</sup> |
| macOS默认 | 自2019年起，zsh一直是macOS的默认外壳<sup>[3](https://myunix.org/guides/unix-shell-history/)</sup> |

## 外壳的职能

从根本上说，外壳是一个执行命令的宏处理器，将文本和符号展开为更大的表达式。Unix外壳既是一个命令解释器，为用户提供访问系统实用工具的界面，又是一种编程语言，其特性允许将这些实用工具组合起来。用户可以创建包含命令的文件，这些文件本身就成为命令，与/bin等目录中的系统命令具有同等地位。<sup>[5](https://www.gnu.org/s/bash/manual/bash.html)</sup>

外壳以两种模式运行。在交互模式下，它们接受从键盘键入的输入；在以非交互方式执行时，它们从文件或字符串中读取命令。它们可以同步运行命令，等待其完成，也可以异步运行，让命令并行继续执行，同时外壳读取更多输入。重定向构造可以精细控制命令的输入和输出，外壳则控制命令运行的环境。<sup>[5](https://www.gnu.org/s/bash/manual/bash.html)</sup>

**内建命令与脚本。** 外壳提供一小组内建命令，实现那些无法通过独立实用工具获得的功能，因为这些功能操作的是外壳本身，例如cd、break、continue和exec。其他如history、getopts、kill和pwd，本可以作为独立程序存在，但作为内建命令更为方便。外壳脚本是包含外壳命令的文本文件；执行时，外壳将特殊参数0设为该文件的名称，并将位置参数设为其余任何参数。<sup>[5](https://www.gnu.org/s/bash/manual/bash.html)</sup>

当外壳执行一个简单命令时，它读取输入，按照引用规则将其分解为单词和操作符，将记号解析为命令，执行展开，实施任何重定向，执行命令，并收集其退出状态。<sup>[5](https://www.gnu.org/s/bash/manual/bash.html)</sup> 非内建于外壳的命令在由fork和exec创建的独立执行环境中运行；父外壳等待子进程结束，然后打印提示符并读取下一条命令。后台进程也很容易在这一框架下实现：当命令行包含&时，外壳干脆不等待。<sup>[7](https://archive.computerhistory.org/resources/access/text/2024/10/102659317-05-01-acc.pdf)</sup>

## 历史

**汤普森外壳与马谢外壳。** 第一个Unix外壳——[肯·汤普森](https://www.edgepedia.org/zh/ken-thompson)的sh——随1971年的第一版Unix一同发布，是一个不具备脚本编写能力的纯命令解释器。这些早期解释器——汤普森外壳、马谢（Mashey）外壳和伯恩外壳——都名为sh，在1970年到1976年期间，它们共存，后来的版本取代先前的版本，反映了持续的改进和功能的扩展。<sup>[3](https://myunix.org/guides/unix-shell-history/)</sup><sup> • </sup><sup>[8](https://www.linuxjournal.com/content/understanding-bash-elements-programming)</sup> 一份汤普森外壳（即Unix命令解释器）的PDP-7汇编清单留存于[计算机历史博物馆](https://www.edgepedia.org/zh/computer-history-museum)发布的最早Unix代码中，其年代可追溯到1970年或1971年初。<sup>[9](https://computerhistory.org/blog/the-earliest-unix-code-an-anniversary-source-code-release/)</sup>

**伯恩外壳。** 重大的一步出现在1979年，当时在[贝尔实验室](https://www.edgepedia.org/zh/bell-labs)的斯蒂芬·伯恩随UNIX第七版推出了伯恩外壳。<sup>[10](https://linuxgratis.com/en/articles/history-of-bash-shell)</sup> 伯恩的外壳包含编程语言特性，如控制流原语、结构化编程技术和内建命令。<sup>[11](https://www.stat.berkeley.edu/~statcur/Workshop2/Presentations/shell.pdf)</sup> 外壳独立于UNIX操作系统本身，这导致在Unix历史上发展出了数十种外壳，尽管只有少数几种获得了广泛使用。<sup>[4](https://web.eecs.utk.edu/~miturria/ksh/ch01_03.htm)</sup> AT&T直到2005年以CDDL发布OpenSolaris源代码时，才将最初的伯恩外壳开源。<sup>[3](https://myunix.org/guides/unix-shell-history/)</sup>

**科恩外壳与C外壳的影响。** 贝尔实验室的大卫·科恩发布了科恩外壳（ksh），有意将伯恩外壳的脚本兼容性与C外壳家族所普及的交互便利——命令历史、别名和作业控制——结合在一个外壳中。<sup>[6](https://danielcosenza.com/posts/sh-history/)</sup> 科恩外壳几乎完全向上兼容伯恩外壳，因此伯恩外壳的用户可以立即采用它，为伯恩外壳编写的系统实用工具也可以在其下运行；一些系统将科恩外壳当作伯恩外壳来安装。<sup>[4](https://web.eecs.utk.edu/~miturria/ksh/ch01_03.htm)</sup> 各资料对其发布日期的记载不尽相同：一种说法给出1983年，<sup>[6](https://danielcosenza.com/posts/sh-history/)</sup> 而科恩外壳一书的历史章节将其发明定于20世纪80年代中期，公开亮相于1986年，作为AT&T“实验工具箱”的一部分，源代码以低价提供给愿意接受不受支持、可能有缺陷的代码的用户。<sup>[4](https://web.eecs.utk.edu/~miturria/ksh/ch01_03.htm)</sup>

**Bash与zsh。** 自由软件基金会为追求一个完整的类Unix系统，从零开始开发了一个伯恩外壳的克隆，名为Bash。<sup>[12](https://www.oreilly.com/library/view/bash-pocket-reference/9781449388669/ch01s02.html)</sup> 该名称是Bourne-Again SHell的缩写，是对斯蒂芬·伯恩的双关，Bash旨在成为POSIX规范（IEEE标准1003.1）中IEEE POSIX外壳与工具部分的合规实现。它几乎能在每个版本的Unix以及少数其他操作系统上运行，并有独立支持的Windows及其他平台移植版。<sup>[5](https://www.gnu.org/s/bash/manual/bash.html)</sup> zsh于2019年成为macOS的默认外壳，外壳历史记录将这一决定追溯到1971年至1979年的贝尔实验室谱系。<sup>[3](https://myunix.org/guides/unix-shell-history/)</sup>

## 标准化与可移植性

当POSIX于1992年以POSIX.2正式标准化外壳行为时，该规范大量借鉴了ksh的既有行为，因此今天所说的“符合POSIX的外壳”实际上意味着一个与ksh已经确立的脚本行为相兼容的外壳。<sup>[6](https://danielcosenza.com/posts/sh-history/)</sup> IEEE将标准的“外壳与实用工具”卷描述为定义了命令解释服务（“外壳”）和常用实用工具程序的标准源代码级接口，以实现应用的可移植性。<sup>[13](https://standards.ieee.org/ieee/1003.1/7101/)</sup> 现行有效的POSIX.1-2024版以IEEE Std 1003.1-2024和The Open Group基础规范第8版两种形式发布。<sup>[13](https://standards.ieee.org/ieee/1003.1/7101/)</sup><sup> • </sup><sup>[14](https://danielcosenza.com/posts/sh-howto-posix-scripting/)</sup>

POSIX是一种规范，而不是一个程序；实际运行脚本的是bash、dash、ash、ksh、yash或其他实现，每种都有自己的缺陷和扩展。<sup>[15](https://alganet.github.io/blog/2026-06-28-12-POSIX-Is-Not-A-Shell.html)</sup> 一个只在Bash下编写和测试的脚本，在像Dash这样严格的POSIX外壳下首次运行时可能会失败，有时会悄无声息地产生错误结果——Dash在Debian、Ubuntu和Alpine上是/bin/sh。<sup>[14](https://danielcosenza.com/posts/sh-howto-posix-scripting/)</sup><sup> • </sup><sup>[15](https://alganet.github.io/blog/2026-06-28-12-POSIX-Is-Not-A-Shell.html)</sup> 就连标准也未对某些行为作出明确规定：echo对反斜杠转义的处理由实现定义，标准鼓励改用printf。<sup>[15](https://alganet.github.io/blog/2026-06-28-12-POSIX-Is-Not-A-Shell.html)</sup>

## 语言设计

命令语言在几个方面不同于通用编程语言。它们优先考虑可组合性：管道操作符（|）让一个命令的输出成为下一个命令的输入，从而能够以小型、单一用途的实用工具实现复杂的数据转换。外部程序被当作一等公民处理，这意味着调用编译后的二进制文件、脚本或外壳内建命令的语法在三种情况下完全相同。默认情况下，变量不携带类型，而当展开式不加引号时，产生的单词会经历拆分和路径名展开——这些规则偏离了大多数编程语言的语义，并经常引发难以察觉的脚本错误。<sup>[16](https://technav.ieee.org/topic/command-languages/)</sup>

POSIX外壳的单词展开在编程语言中很不寻常：命令语言不求值子表达式，而是将它们视为字符串并展开其中的特殊控制码，将~转换为家目录，替换参数，执行命令替换，并用*和?进行通配。POSIX外壳是仍被广泛使用的最古老的编程语言之一，现代容器系统依赖它，有些大量使用（Docker），另一些则将其作为必要的应急手段（Vagrant、Puppet）。<sup>[17](https://arxiv.org/abs/1907.05308)</sup>

**作为对照的PowerShell。** [PowerShell](https://www.edgepedia.org/zh/powershell)是一种用于自动化的命令行外壳和脚本语言；像bash或cmd.exe一样，它可以运行系统上可用的任何命令，而不仅仅是自己的命令。<sup>[18](https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/running-commands)</sup> 它的管道有本质上的不同：命令将结构化对象传递给后继命令，这些对象在可用时逐个传递，而不是传递文本行。<sup>[19](https://learn.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-03?view=powershell-7.6)</sup> 它的管道链操作符类似于bash、zsh和sh等POSIX外壳中的AND-OR列表。<sup>[20](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_pipeline_chain_operators)</sup>

## 形式研究与工具

由于外壳是关键但微妙的基础设施，研究人员为其建立了形式模型。Smoosh是一种针对POSIX外壳的机械化可执行小步语义，使用三套测试套件将其与其他七个旨在符合POSIX的外壳（bash、dash、zsh、OSH、mksh、ksh93和yash）进行了比较，其语义被证明最符合POSIX标准；这项工作还发现了正在使用中的外壳以及POSIX规范及其测试套件中的错误。<sup>[17](https://arxiv.org/abs/1907.05308)</sup> 其他近期工作包括基于语法的外壳解释器模糊测试，它暴露了mksh外壳中此前未知的问题，<sup>[21](https://arxiv.org/abs/2408.00433)</sup> 以及PaSh等系统，通过将POSIX外壳脚本转换为数据流图再转换回来来实现并行化。<sup>[22](https://arxiv.org/abs/2007.09436)</sup>

## References

1. [Shell Command Language, POSIX.1-2017, The Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html)
2. [S. R. Bourne, "The UNIX Shell," Bell Laboratories (manuscript received January 30, 1978), Nokia Bell Labs publications](https://www.nokia.com/bell-labs/publications-and-media/publications/unix-time-sharing-system-the-unix-shell/)
3. [Unix Shell History: sh, csh, ksh, bash, and zsh, MyUnix.org](https://myunix.org/guides/unix-shell-history/)
4. [History of UNIX Shells, Korn shell book chapter 1.3, University of Tennessee](https://web.eecs.utk.edu/~miturria/ksh/ch01_03.htm)
5. [Bash Reference Manual, GNU Project, Free Software Foundation](https://www.gnu.org/s/bash/manual/bash.html)
6. [The History of the Unix Shell: From Thompson Shell to Bash and Zsh, Daniel Cosenza](https://danielcosenza.com/posts/sh-history/)
7. [Documents for use with the UNIX time-sharing system, 6th edition, 1971-1975, Computer History Museum archive](https://archive.computerhistory.org/resources/access/text/2024/10/102659317-05-01-acc.pdf)
8. [Understanding Bash: Elements of Programming, Linux Journal](https://www.linuxjournal.com/content/understanding-bash-elements-programming)
9. [The Earliest Unix Code: An Anniversary Source Code Release, Computer History Museum](https://computerhistory.org/blog/the-earliest-unix-code-an-anniversary-source-code-release/)
10. [The History of Bash and the Unix Shell: From Thompson to Today, LinuxGratis](https://linuxgratis.com/en/articles/history-of-bash-shell)
11. [Shell, Shell Scripting and Linux System, UC Berkeley presentation](https://www.stat.berkeley.edu/~statcur/Workshop2/Presentations/shell.pdf)
12. [History, bash Pocket Reference, O'Reilly](https://www.oreilly.com/library/view/bash-pocket-reference/9781449388669/ch01s02.html)
13. [IEEE/Open Group 1003.1-2017 and 1003.1-2024, IEEE Standards Association](https://standards.ieee.org/ieee/1003.1/7101/)
14. [How to Write Robust, Portable POSIX Shell Scripts, Daniel Cosenza](https://danielcosenza.com/posts/sh-howto-posix-scripting/)
15. [POSIX Is Not A Shell, Alexandre Gomes Gaigalas](https://alganet.github.io/blog/2026-06-28-12-POSIX-Is-Not-A-Shell.html)
16. [Command languages, IEEE Technology Navigator](https://technav.ieee.org/topic/command-languages/)
17. [Executable formal semantics for the POSIX shell (Smoosh), arXiv:1907.05308](https://arxiv.org/abs/1907.05308)
18. [Running commands in the shell, PowerShell documentation, Microsoft Learn](https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/running-commands)
19. [Basic concepts, PowerShell language specification, Microsoft Learn](https://learn.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-03?view=powershell-7.6)
20. [about_Pipeline_Chain_Operators, PowerShell documentation, Microsoft Learn](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_pipeline_chain_operators)
21. [ShellFuzzer: Grammar-based Fuzzing of Shell Interpreters, arXiv:2408.00433](https://arxiv.org/abs/2408.00433)
22. [PaSh: Light-touch Data-Parallel Shell Processing, arXiv:2007.09436](https://arxiv.org/abs/2007.09436)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: 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/shell-computing

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