Print STDOUT/STDERR and write them to a file in Bash?

纵饮孤独 提交于 2020-01-01 09:18:15

问题


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


来源:https://stackoverflow.com/questions/4739418/print-stdout-stderr-and-write-them-to-a-file-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!