简体   繁体   English

打印STDOUT / STDERR并将它们写入Bash中的文件?

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

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

This will redirect both STDOUT and STDERR to the same file: 这会将STDOUT和STDERR重定向到同一个文件:

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. 使用tee命令。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM