【发布时间】:2023-03-23 02:51:01
【问题描述】:
有没有办法让 Bash 将 STDOUT/STDERR 重定向到文件但仍将它们打印到终端?
【问题讨论】:
标签: linux bash stdout stderr io-redirection
有没有办法让 Bash 将 STDOUT/STDERR 重定向到文件但仍将它们打印到终端?
【问题讨论】:
标签: linux bash stdout stderr io-redirection
这会将 STDOUT 和 STDERR 重定向到同一个文件:
some_command 2>&1 | tee file.log
$ touch foo; ls foo asfdsafsadf 2>&1 | tee file.log
ls: asfdsafsadf: No such file or directory
foo
$ cat file.log
ls: asfdsafsadf: No such file or directory
foo
【讨论】:
file.log,请使用tee -a file.log
使用tee 命令。
$ echo "hi" | tee output.txt
hi
[unix]$ ls
output.txt
[unix]$ cat output.txt
hi
【讨论】: