9

有没有办法让 Bash 将 STDOUT/STDERR 重定向到文件但仍然将它们打印到终端?

4

2 回答 2

17

这会将 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
于 2011-01-19T19:00:37.100 回答
3

使用tee命令。

$ echo "hi" | tee output.txt
hi
[unix]$ ls
output.txt
[unix]$ cat output.txt 
hi
于 2011-01-19T19:00:19.927 回答