# Bourne shell

Bourne shell（`sh`）是 Unix 的命令解释器和脚本语言，由斯蒂芬·R·伯恩（Stephen R. Bourne）在[贝尔实验室](https://www.edgepedia.org/zh/bell-labs)（Bell Labs）编写，随 1979 年发布的 Unix 第七版（V7）引入。<sup>[1](https://www.gnu.org/software/bash/manual/bash.html)</sup><sup> • </sup><sup>[2](https://citeseerx.ist.psu.edu/document?doi=274c3b848462f54ac0592bcd02a243dd66a2ef6a&repid=rep1&type=pdf)</sup> 它取代了此前的各种 shell，成为标准的 [Unix shell](https://www.edgepedia.org/zh/unix-shell)，并成为当今大多数在用 shell 的直接祖先，包括 Korn shell 和 Bash——后者的名称（“Bourne-Again SHell”）是对伯恩本人姓氏的一个双关。<sup>[1](https://www.gnu.org/software/bash/manual/bash.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 作者 | 贝尔实验室的斯蒂芬·R·伯恩；该 shell 以他的名字命名<sup>[1](https://www.gnu.org/software/bash/manual/bash.html)</sup> |
| 首次发布 | 第七版贝尔实验室研究 Unix（V7），1979 年发行<sup>[1](https://www.gnu.org/software/bash/manual/bash.html)</sup><sup> • </sup><sup>[2](https://citeseerx.ist.psu.edu/document?doi=274c3b848462f54ac0592bcd02a243dd66a2ef6a&repid=rep1&type=pdf)</sup> |
| 设计描述 | 伯恩 1978 年发表于《贝尔系统技术期刊》的论文将 shell 描述为既是命令解释器，又是一种具有控制流、变量和参数传递的编程语言<sup>[3](https://www.centerconsulting.com/code-library/tools/bourne-shell)</sup> |
| 最后一个历史版本 | SVR4.2 中所含的 `sh`，被用作兼容性比较的基线参照<sup>[4](https://www.gnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html)</sup> |
| 标准化 | POSIX shell 命令语言（IEEE Std 1003.1）以 System V shell 为基础<sup>[5](https://mirror.math.princeton.edu/pub/oldlinux/Linux.old/Ref-docs/POSIX/all.pdf)</sup> |
| 现代后裔 | Bash、Korn shell（`ksh`），以及 `dash`——一个符合 POSIX 的 `/bin/sh`，源自 Almquist shell<sup>[1](https://www.gnu.org/software/bash/manual/bash.html)</sup><sup> • </sup><sup>[6](https://manpages.debian.org/bullseye/dash/sh.1.en.html)</sup> |

## 目的与设计

在 1978 年为《贝尔系统技术期刊》撰写的文章《UNIX 分时系统：UNIX shell》中，伯恩将 shell 的职能概括为两项：它解释命令，或以交互方式，或以文件作为输入；它同时充当一种编程语言，提供控制流、变量和参数传递。<sup>[3](https://www.centerconsulting.com/code-library/tools/bourne-shell)</sup> `sh` 的第七版手册页精确列举了 shell 的行为：它如何解析命令、执行文件名生成、处理引用，以及运行其内建命令。<sup>[3](https://www.centerconsulting.com/code-library/tools/bourne-shell)</sup>

Bourne shell 所定义的语言至今仍是 POSIX shell 脚本的核心。脚本是一个包含命令的文件；shell 读取各行，在空白处以及 `|`、`;`、`&` 等操作符字符处将其拆分为词，然后执行。使用单引号、双引号和反斜杠进行引用可去除字符的特殊含义。控制结构包括 `if`/`then`/`elif`/`else`/`fi`、`while` 和 `until` 循环、对词进行遍历的 `for` 循环，以及 `case` 语句，命令可用 `(list)` 在子 shell 中分组，或用 `{ list; }` 在当前 shell 中分组。<sup>[7](https://manpages.debian.org/bookworm/dash/sh.1.en.html)</sup> 管道将一个命令的标准输出连接到下一个命令的标准输入，每个命令都有一个退出状态——零表示成功，非零表示失败——通过 `&&` 和 `||` 操作符驱动条件执行。<sup>[7](https://manpages.debian.org/bookworm/dash/sh.1.en.html)</sup>

**变量与展开。** shell 在启动时将环境变量转为 shell 变量，并用 `name=value` 设置新变量。位置参数（`$1`、`$2` 等）保存脚本的参数，特殊参数如 `$?`（最近一个管道的退出状态）和 `$#`（位置参数的个数）暴露 shell 的状态。参数展开的形式为 `${parameter}`，可带修饰符，如 `${parameter:-word}` 在参数未设置或为空时替换为默认值。<sup>[7](https://manpages.debian.org/bookworm/dash/sh.1.en.html)</sup>

## 在 Unix 历史中的地位

1979 年随 V7 Unix 发行的原始 Bourne shell 成为 Unix 的标准 shell。<sup>[2](https://citeseerx.ist.psu.edu/document?doi=274c3b848462f54ac0592bcd02a243dd66a2ef6a&repid=rep1&type=pdf)</sup> 同样在贝尔实验室的大卫·科恩（David Korn）是第一位增强 Bourne shell 的开发者，他添加了 [C shell](https://www.edgepedia.org/zh/c-shell) 风格的功能，如命令历史和作业控制，从而产生了 Korn shell（`ksh`）。<sup>[2](https://citeseerx.ist.psu.edu/document?doi=274c3b848462f54ac0592bcd02a243dd66a2ef6a&repid=rep1&type=pdf)</sup>

当 POSIX 标准的制定者规定一种 shell 命令语言时，他们选择 System V shell 作为基础，但并未将标准局限于它的向上兼容扩展。<sup>[5](https://mirror.math.princeton.edu/pub/oldlinux/Linux.old/Ref-docs/POSIX/all.pdf)</sup> 单一 UNIX 规范的 shell 命令语言即标准的 POSIX shell。<sup>[8](https://www.unix.org/overview.html)</sup> 历史性的 Bourne shell 本身的最后一个版本是 SVR4.2 中所含的 `sh`。<sup>[4](https://www.gnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html)</sup>

## 后裔与兼容性

**Bash。** Bash 是 GNU 操作系统的默认 shell，它实现的语法、参数与变量展开、重定向和引用基本上与 Bourne shell 相同，并以 POSIX 标准作为这些特性行为的规范。<sup>[4](https://www.gnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html)</sup> 它增加了历史性 shell 所没有的交互和编程功能：命令行编辑、进程替换、推荐用来代替旧式反引号形式的 POSIX `$()` 命令替换形式、使用 `${!word}` 的间接展开，以及 Bourne shell 所没有的 `RANDOM` 和 `BASH_VERSION` 等变量。<sup>[4](https://www.gnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html)</sup> Bash 的手册将这些记录为“与 Bourne shell 的主要差异”，并以 SVR4.2 的 shell 作为基线。<sup>[4](https://www.gnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html)</sup>

**dash 与 Almquist shell。** 在 Debian 及类似系统上，`/bin/sh` 通常是 `dash`，这是一个符合 POSIX 的 shell 实现，是 NetBSD 版 `ash`（Almquist SHell）的直接后裔，于 1997 年初移植到 Linux，力求尽可能小。<sup>[6](https://manpages.debian.org/bullseye/dash/sh.1.en.html)</sup> Debian 的 wiki 将 `dash` 和 `busybox` 列为其使用的 Bourne 兼容脚本解释器。<sup>[9](https://wiki.debian.org/BourneShell)</sup>

**可移植性实践。** 由于 POSIX shell 语言源自 Bourne shell，用其公共子集编写的脚本可在各 Unix 系统上运行，而 `/bin/sh` 在符合 POSIX 的系统上作为默认的非交互式 shell 存在。<sup>[10](https://wiki.debian.org/Shell)</sup> 反方向的兼容并不完全：Autoconf 的文档指出，厂商会避免破坏现有的 shell 脚本，而 Bourne shell 中的一些边角情况与 POSIX shell 并不完全兼容。<sup>[11](https://ftp.gnu.org/old-gnu/Manuals/autoconf-2.53/html_node/Shellology.html)</sup>

## 遗产

Bourne shell 的语法、引用规则、重定向操作符和控制结构在 POSIX shell 标准以及实现该标准的每一个 shell 中基本原样存留，从 Bash 和 `ksh` 到 `dash` 和 BusyBox。第七版的 `sh` 被视为贝尔实验室产出的重要软件之一，与 Unix 本身以及伴随它成长的 C 语言并列。<sup>[3](https://www.centerconsulting.com/code-library/tools/bourne-shell)</sup>

## References

1. [What is Bash?, GNU Bash Reference Manual](https://www.gnu.org/software/bash/manual/bash.html)
2. [A History of the Shell (CiteSeerX history document)](https://citeseerx.ist.psu.edu/document?doi=274c3b848462f54ac0592bcd02a243dd66a2ef6a&repid=rep1&type=pdf)
3. [The Bourne Shell (sh), CenterConsulting Code Library](https://www.centerconsulting.com/code-library/tools/bourne-shell)
4. [Major Differences From The Bourne Shell, GNU Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html)
5. [P1003.2/D11.2, POSIX shell draft](https://mirror.math.princeton.edu/pub/oldlinux/Linux.old/Ref-docs/POSIX/all.pdf)
6. [sh(1) — dash, Debian Manpages](https://manpages.debian.org/bullseye/dash/sh.1.en.html)
7. [sh(1) — dash — debian bookworm — debian manpages](https://manpages.debian.org/bookworm/dash/sh.1.en.html)
8. [The Single UNIX Specification V5 (2024) — Introduction](https://www.unix.org/overview.html)
9. [BourneShell, Debian Wiki](https://wiki.debian.org/BourneShell)
10. [Shell, Debian Wiki](https://wiki.debian.org/Shell)
11. [Shellology, GNU Autoconf Manual](https://ftp.gnu.org/old-gnu/Manuals/autoconf-2.53/html_node/Shellology.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Unix-like and Linux › Unix, BSD, and kernel internals

Copyright 2026 Edgepedia

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

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

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