在 bash 中,调用 foo
会在标准输出上显示该命令的任何输出。
调用 foo > output
会将来自该命令的任何输出重定向到指定的文件(在本例中为“输出”)。
有没有办法将输出重定向到一个文件并让它显示在标准输出上?
最佳答案
你想要的命令被命名为 tee
:
foo | tee output.file
例如,如果你只关心标准输出:
ls -a | tee output.file
如果您想包含标准错误,请执行以下操作:
program [arguments...] 2>&1 | tee outfile
2>&1
将 channel 2(stderr/标准错误)重定向到 channel 1(stdout/标准输出),这样两者都写为 stdout。从 tee
命令开始,它也被定向到给定的输出文件。
此外,如果您想追加到日志文件,请使用 tee -a
作为:
program [arguments...] 2>&1 | tee -a outfile
https://stackoverflow.com/questions/418896/