> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/cat.1.md).

# cat.1

`cat` — 串接并打印文件

## 名称

`cat`

## 概要

`cat [-belnstuv] [file]`

## 描述

`cat` 实用程序按顺序读取文件，并将其写入标准输出。`file` 操作数按命令行顺序处理。如果 `file` 是单个短横线（“`-`”）或未指定，`cat` 从标准输入读取。如果 `file` 是一个 UNIX 域套接字，`cat` 会连接到该套接字并读取直到 `EOF`。这一功能补充了 [inetd(8)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man8/inetd.8.md) 中可用的 UNIX 域绑定能力。

选项如下：

**`-b`** 对非空输出行编号，从 1 开始。

**`-e`** 显示不可打印字符（参见 `-v` 选项），并在每行末尾显示美元符号（`$`）。

**`-l`** 在标准输出文件描述符上设置独占咨询锁。该锁通过 fcntl(2) 的 `F_SETLKW` 命令设置。如果输出文件已被锁定，`cat` 将阻塞直到获得锁。

**`-n`** 对输出行编号，从 1 开始。

**`-s`** 压缩多个相邻的空行，使输出为单倍行距。

**`-t`** 显示不可打印字符（参见 `-v` 选项），并将制表符显示为 `^I`。

**`-u`** 禁用输出缓冲。

**`-v`** 显示不可打印字符以使其可见。控制字符显示为 `^X`（表示 control-X）；删除字符（八进制 0177）显示为 `^?`。非 ASCII 字符（高位被设置）显示为 `M-`（表示 meta）后跟低 7 位对应的字符。

## 退出状态

`cat` 实用程序成功时退出值为 0，发生错误时大于 0。

## 实例

命令：

```sh
cat file1
```

将 `file1` 的内容打印到标准输出。

命令：

```sh
cat file1 file2 > file3
```

将 `file1` 和 `file2` 的内容依次打印到文件 `file3` 中，如果 `file3` 已存在则截断之。有关重定向的更多信息，请参见你所使用的 shell 的手册页（例如 [sh(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/sh.1.md)）。

命令：

```sh
cat file1 - file2 - file3
```

将打印 `file1` 的内容，然后打印从标准输入接收的数据直到收到 `EOF`（“^D”）字符，接着打印 `file2` 的内容，再次读取并输出标准输入的内容，最后输出 `file3` 的内容。注意，如果标准输入指向一个文件，命令行上的第二个短横线将不起作用，因为在遇到第一个“`-`”操作数时，`cat` 已经读取并打印了该文件的全部内容。

## 参见

[head(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/head.1.md), [more(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/more.1.md), pr(1), [sh(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/sh.1.md), [tail(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/tail.1.md), vis(1), [zcat(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/zcat.1.md), fcntl(2), setbuf(3)

> Rob Pike, "UNIX Style, or cat -v Considered Harmful", *USENIX Summer Conference Proceedings*, 1983.

## 标准

`cat` 实用程序符合 ISO/IEC 9945-2:1993 ("POSIX.2") 规范。

标志 \[`-belnstv`] 是对该规范的扩展。

## 历史

`cat` 实用程序首次出现于 Version 1 AT\&T UNIX。Dennis Ritchie 设计并编写了第一个手册页，该手册页似乎就是为 `cat` 编写的。

## 缺陷

由于执行输出重定向所使用的 shell 语言机制，命令“`cat file1 file2 > file1`”会导致 `file1` 中的原始数据被破坏！

当 `-t` 或 `-v` 选项生效时，`cat` 实用程序无法识别多字节字符。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/cat.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
