Is there a way to have Bash redirect STDOUT/STDERR to a file yet still print them out to the terminal as well?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
This will redirect both STDOUT and STDERR to the same file:
some_command 2>&1 | tee file.log
Example
$ 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
回答2:
Use the tee command.
$ echo "hi" | tee output.txt
hi
[unix]$ ls
output.txt
[unix]$ cat output.txt
hi