国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > cat 以获得更好的体验。

# Bash 的 cat 命令

cat [OPTION]... [FILE]...

!subtitle:功能

拼接文件内容并打印到标准输出。

!subtitle:类型

可执行文件(/usr/bin/cat),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -A, --show-all - 显示所有特殊符号;同 -vET

    • -b, --number-nonblank - 非空行显示行号

    • -e - 行末显示 $,其它不可打印字符显示为 ^M- 前缀形式;同 -vE

    • -E, --show-ends - 行末显示 $

    • -n, --number - 显示行号

    • -s, --squeeze-blank - 抑制重复的空行

    • -t - 制表符显示为 ^I,其它不可打印字符显示为 ^M- 前缀形式;同 -vT

    • -T, --show-tabs - 制表符显示为 ^I

    • -u - 不进行缓冲;已废弃,被忽略

    • -v, --show-nonprinting - 不可打印字符显示为 ^M- 前缀形式,参考 ASCII 编码表

    • --help - 显示帮助

    • --version - 显示版本

  • FILE - 要拼接的文件列表,如果没有这个参数或指定为 -,则读取标准输入

# 示例

!subtitle:查看文件

$ cat file          # 查看 file
$ cat file1 file2   # 查看 file1 和 file2

!subtitle:写文件

$ cat > file        # 将标准输入写入 file
  • 将标准输入覆盖写入 file 文件

  • Ctrl + D 结束

$ cat >> file        # 将标准输入写入 file
  • 将标准输入追加写入 file 文件

  • Ctrl + D 结束

$ cat src1 src2 > dest    # 将 src1 和 src2 拼接,写入 dest
  • 拼接 src1src2,覆盖写入 dest

# 推荐阅读

# 手册

CAT(1)                           User Commands                          CAT(1)

NAME
       cat - concatenate files and print on the standard output

SYNOPSIS
       cat [OPTION]... [FILE]...

DESCRIPTION
       Concatenate FILE(s) to standard output.

       With no FILE, or when FILE is -, read standard input.

       -A, --show-all
              equivalent to -vET

       -b, --number-nonblank
              number nonempty output lines, overrides -n

       -e     equivalent to -vE

       -E, --show-ends
              display $ at end of each line

       -n, --number
              number all output lines

       -s, --squeeze-blank
              suppress repeated empty output lines

       -t     equivalent to -vT

       -T, --show-tabs
              display TAB characters as ^I

       -u     (ignored)

       -v, --show-nonprinting
              use ^ and M- notation, except for LFD and TAB

       --help display this help and exit

       --version
              output version information and exit

EXAMPLES
       cat f - g
              Output f's contents, then standard input, then g's contents.

       cat    Copy standard input to standard output.

AUTHOR
       Written by Torbjorn Granlund and Richard M. Stallman.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2023  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       tac(1)

       Full documentation <https://www.gnu.org/software/coreutils/cat>
       or available locally via: info '(coreutils) cat invocation'

GNU coreutils 9.4                 April 2024                            CAT(1)
本文 更新于: 2025-11-27 09:38:14 创建于: 2025-11-27 09:38:14