3416

17 分钟

#Bash 的 tee 命令

tee [OPTION]... [FILE]...

功能

读取标准输入,然后将内容打印到标准输出以及每个文件。

类型

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

参数

  • OPTION 选项:
    • -a, --append - 追加写入文件,而不是覆盖文件
    • -i, --ignore-interrupts - 忽略中断信号
    • -p - 以更合适的模式操作管道
    • --output-error[=MODE] - 写入错误时的行为:
      • warn - 写入错误时发出警告
      • warn-nopipe - 写入非管道输出发生错误时,发出警告
      • exit - 写入错误时退出
      • exit-nopipe - 写入非管道输出发生错误时,退出
    • --help - 显示帮助
    • --version - 显示版本
  • FILE - 文件列表;如果没有这个参数或指定为 -,则读取标准输入

#示例

创建文件

$ tee 1.txt << EOF # 将接下来的输入写入 1.txt,遇到 EOF 时结束 > Primers 编程伙伴 > https://xplanc.org/primers/ > EOF Primers 编程伙伴 https://xplanc.org/primers/ $ cat 1.txt Primers 编程伙伴 https://xplanc.org/primers/

cmd << XXX 语法表示读取输入直到 XXX 结束,然后将这段内容(不含 XXX)作为标准输入运行 cmd

重定向

$ my_cmd | tee -a 1.txt # 将 my_cmd 命令的输出打印出来的同时追加写入 1.txt

#推荐阅读

#手册

TEE(1) User Commands TEE(1) NAME tee - read from standard input and write to standard output and files SYNOPSIS tee [OPTION]... [FILE]... DESCRIPTION Copy standard input to each FILE, and also to standard output. -a, --append append to the given FILEs, do not overwrite -i, --ignore-interrupts ignore interrupt signals -p operate in a more appropriate MODE with pipes. --output-error[=MODE] set behavior on write error. See MODE below --help display this help and exit --version output version information and exit MODE determines behavior with write errors on the outputs: warn diagnose errors writing to any output warn-nopipe diagnose errors writing to any output not a pipe exit exit on error writing to any output exit-nopipe exit on error writing to any output not a pipe The default MODE for the -p option is 'warn-nopipe'. With "nopipe" MODEs, exit immediately if all outputs become broken pipes. The de‐ fault operation when --output-error is not specified, is to exit imme‐ diately on error writing to a pipe, and diagnose errors writing to non pipe outputs. AUTHOR Written by Mike Parker, Richard M. Stallman, and David MacKenzie. 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 Full documentation <https://www.gnu.org/software/coreutils/tee> or available locally via: info '(coreutils) tee invocation' GNU coreutils 9.4 April 2024 TEE(1)

创建于 2025/12/5

更新于 2025/12/5