开发者

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

开发者 https://www.devze.com 2023-02-05 07:49 出处:网络
Is there a way to have Bash redirect STDOUT/STDERR to a file yet still print开发者_StackOverflow them out to the terminal as well?This will redirect both STDOUT and STDERR to the same file:

Is there a way to have Bash redirect STDOUT/STDERR to a file yet still print开发者_StackOverflow them out to the terminal as well?


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


Use the tee command.

$ echo "hi" | tee output.txt
hi
[unix]$ ls
output.txt
[unix]$ cat output.txt 
hi
0

精彩评论

暂无评论...
验证码 换一张
取 消