How to redirect output both to screen and file, separate stderr and stdout etc.
Linux shell has 3 file descriptors:
- stdin (0)
- stdout (1)
- stderr (2)
Redirect stderr andĀ stdoutĀ to file
Redirect stdout to file:
1 |
$ echo "mytest" > myout.txt |
Redirect stderr to file:
1 |
$ echo "mytest" 2> myerr.txt |
Redirect both to the same file:
1 |
$ echo "mytest" > mystdout.txt 2>&1 |
Redirect stdout and stderr to different files:
1 |
$ echo "mytest" > myout.txt 2> myerr.txt |
Redirect output to both file and screen
1 |
$ echo "mytest" |& tee mylog.txt |
or
1 |
$ echo "mytest" 2>&1 | tee mylog.txt |