且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将输出重定向到文件和标准输出

更新时间:2023-01-06 20:10:28

你想要的命令名为 tee:

foo | tee output.file

例如,如果您只关心标准输出:

For example, if you only care about stdout:

ls -a | tee output.file

如果要包含 stderr,请执行以下操作:

If you want to include stderr, do:

program [arguments...] 2>&1 | tee outfile

2>&1 将通道 2(stderr/标准错误)重定向到通道 1(stdout/标准输出),这样两者都被写入为 stdout.它也被定向到 tee 命令的给定输出文件.

2>&1 redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee command.

此外,如果您想附加到日志文件,请使用 tee -a 为:

Furthermore, if you want to append to the log file, use tee -a as:

program [arguments...] 2>&1 | tee -a outfile