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

Is there a way for Bash to redirect STDOUT / STDERR to a file, but still print them to the terminal?

+7
source share
2 answers

This will redirect 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 
+16
source

Use the tee command.

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

All Articles