# tee（命令）

**tee** 是 Unix 的标准命令行工具，它将标准输入复制到标准输出，同时复制到零个或多个以参数形式指定的文件中。当数据既需要继续流向下一命令、又需要保存到文件时，通常将其置于管道中；其名称来自管道接头处的 T 形分流。<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup><sup> • </sup><sup>[2](https://man.freebsd.org/cgi/man.cgi?manpath=FreeBSD+14.2-RELEASE&query=tee&sektion=1)</sup>

| 关键事实 | 详情 |
|---|---|
| 功能 | 将标准输入复制到标准输出及每个文件参数；不存在的文件会被创建，已存在的文件会被覆盖，除非指定 `-a`<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup> |
| 用法概要 | `tee [option]... [file]...`<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup> |
| 首次出现 | Version 7 AT&T UNIX<sup>[2](https://man.freebsd.org/cgi/man.cgi?manpath=FreeBSD+14.2-RELEASE&query=tee&sektion=1)</sup> |
| 标准选项 | `-a`（追加）、`-i`（忽略 SIGINT）由 POSIX 规定<sup>[3](https://www.linuxman7.org/linux/man-pages/man1/tee.1p.html)</sup> |
| GNU 扩展 | `-p` 和 `--output-error[=MODE]` 控制写入错误时的行为，模式有 `warn`、`warn-nopipe`、`exit` 和 `exit-nopipe`<sup>[4](https://manpages.debian.org/unstable/coreutils/tee.1.en.html)</sup> |
| 退出状态 | 零表示成功；非零表示失败<sup>[2](https://man.freebsd.org/cgi/man.cgi?manpath=FreeBSD+14.2-RELEASE&query=tee&sektion=1)</sup> |
| GNU 实现 | 由 Mike Parker、Richard M. Stallman 和 David MacKenzie 编写，属于 GNU Coreutils<sup>[4](https://manpages.debian.org/unstable/coreutils/tee.1.en.html)</sup> |

## 基本行为

该命令读取标准输入，并将每个字节写入标准输出及每个文件操作数。如果输出文件不存在则创建；如果存在，则其先前内容会被覆盖，除非使用 `-a`（`--append`）选项，此时输入会被追加。<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup> FreeBSD 的实现说明其输出是无缓冲的，因此数据一经读取便出现在每个目标处。<sup>[2](https://man.freebsd.org/cgi/man.cgi?manpath=FreeBSD+14.2-RELEASE&query=tee&sektion=1)</sup>

FreeBSD 手册中的一个最小示例将回显的消息同时发送到终端和文件：<sup>[2](https://man.freebsd.org/cgi/man.cgi?manpath=FreeBSD+14.2-RELEASE&query=tee&sektion=1)</sup>

```
echo "Hello" | tee greetings.txt
```

`-i` 选项使 tee 忽略中断信号（SIGINT），因此当管道仍在运行时，针对前台进程组的中断不会终止复制。<sup>[3](https://www.linuxman7.org/linux/man-pages/man1/tee.1p.html)</sup>

## GNU tee 的写入错误处理

GNU Coreutils 的 tee 增加了 `-p` 选项和长格式 `--output-error[=MODE]`，用于控制写入某个输出失败时的行为，这在 tee 向可能提前退出的进程馈送数据时很重要。<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup> 四种模式为：<sup>[4](https://manpages.debian.org/unstable/coreutils/tee.1.en.html)</sup>

- `warn`：对任何输出（包括管道）上的错误进行诊断，并继续写入仍打开的输出。
- `warn-nopipe`：同上，但不诊断管道上的错误；这是 `-p` 的默认模式。
- `exit`：写入任何输出（包括管道）出错时退出。
- `exit-nopipe`：除管道破裂外，任何输出出错时退出。

使用“nopipe”模式时，如果所有剩余输出都变成破裂的管道，tee 立即退出。当完全未指定 `--output-error` 时，默认操作是写入管道出错时立即退出，并对写入非管道输出的错误进行诊断。<sup>[4](https://manpages.debian.org/unstable/coreutils/tee.1.en.html)</sup> POSIX 规范采取中间立场：如果写入某个成功打开的文件失败，对其他文件和标准输出的写入继续进行，但退出状态为非零。<sup>[5](https://manpages.ubuntu.com/manpages/stonking/man1/tee.1posix.html)</sup>

## `-` 操作数

在 GNU Coreutils 5.3.0 至 8.23 版本中，文件参数 `-` 会使 tee 将输入的又一份副本发送到标准输出。由于产生的交错输出不太有用，后续版本遵循 POSIX，将 `-` 视为普通文件名。<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup>

## 典型用法

**保存管道输出而无需重新读取。** GNU 手册的动机示例是验证大文件下载。先下载再计算校验和意味着等待下载完成并第二次读取镜像。使用 tee，校验和计算与下载交错进行：<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup>

```
wget -O - https://example.com/dvd.iso \
  | tee dvd.iso | cksum -a sha2 -l 256 > dvd.sha256
```

此处 tee 将镜像写入 `dvd.iso`，同时将同一数据流沿管道传递给校验和程序，因此校验和计算与传输并行进行。<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup>

**进程替换。** 现代 shell 如 `zsh`、`bash` 和 `ksh` 支持进程替换，即 `>(command)` 语法，它将命令连接到[命名管道](https://www.edgepedia.org/zh/named-pipe)；`/bin/sh` 不支持。这使 tee 能同时向多个进程馈送数据，例如在写入镜像文件的同时并行计算 SHA-256 和 SHA3-256 校验和：<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup>

```
wget -O - https://example.com/dvd.iso \
  | tee >(cksum -a sha2 -l 256 > dvd.sha256) \
        >(cksum -a sha3 -l 256 > dvd.sha3) \
  > dvd.iso
```

如果参与进程替换的任何进程可能提前退出而未消费所有数据，则需要 `-p` 选项，以便 tee 继续为剩余输出处理输入。<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup> 同样的技术可以在另一命令消费管道内容的同时对其进行压缩，如 `du -ak | tee >(gzip -9 > /tmp/du.gz) | checkspace -a`，并且可以在 automake 生成的规则中，用一次读取归档后并行压缩来替代顺序的 `gzip` 和 `bzip2` 压缩包创建。<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup>

**写入特权文件。** 由于 shell 重定向由 shell 本身执行，在命令前加 `sudo` 并不会赋予重定向提升的权限。

## 标准与实现

tee 由 POSIX 规定：该工具将标准输入复制到标准输出，并在零个或多个文件中制作副本，接受 `-a` 和 `-i` 选项。<sup>[5](https://manpages.ubuntu.com/manpages/stonking/man1/tee.1posix.html)</sup> FreeBSD 手册说明该工具预期与 IEEE Std 1003.2（“POSIX.2”）兼容，除使用 `-i` 外对所有信号采取默认动作。<sup>[2](https://man.freebsd.org/cgi/man.cgi?manpath=FreeBSD+14.2-RELEASE&query=tee&sektion=1)</sup> GNU 实现是 GNU Coreutils 的一部分，其手册将 tee 描述为唯一由单独命令而非 shell 执行的重定向。<sup>[1](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)</sup> GNU Awk 用户指南附带了一个小型 tee 程序作为示例，称其为“管道接头”，将其输入复制到命令行指定的文件中。<sup>[6](https://www.gnu.org/s/gawk/manual/html_node/Tee-Program.html)</sup>

## References

1. [tee invocation, GNU Coreutils 9.11 manual](https://www.gnu.org/s/coreutils/manual/html_node/tee-invocation.html)
2. [TEE(1), FreeBSD 14.2-RELEASE Manual Pages](https://man.freebsd.org/cgi/man.cgi?manpath=FreeBSD+14.2-RELEASE&query=tee&sektion=1)
3. [tee(1p), POSIX manual page (Linux man-pages mirror)](https://www.linuxman7.org/linux/man-pages/man1/tee.1p.html)
4. [tee(1), GNU Coreutils, Debian Manpages](https://manpages.debian.org/unstable/coreutils/tee.1.en.html)
5. [tee, POSIX.1-2017 (Ubuntu manpage mirror)](https://manpages.ubuntu.com/manpages/stonking/man1/tee.1posix.html)
6. [Tee Program, The GNU Awk User's Guide](https://www.gnu.org/s/gawk/manual/html_node/Tee-Program.html)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/tee-command

English original: https://www.edgepedia.org/tee-command
