Bash - Redirecting stdout and stderr
14 Mar 2016Redirect stdout
/stderr
to a truncated file:
# StdOut
cmd > out.txt # (stderr printed to console)
# StdErr
cmd 2> err.txt # (stdout printed to console)
Redirect stdout
/stderr
to a file (appending):
# StdOut
cmd >> out.txt # (stderr printed to console)
# StdErr
cmd 2>> err.txt # (stdout printed to console)
Redirect both stdout
and stderr
in same command to different files (truncating):
cmd > out.txt 2> err.txt
Redirect both stdout
and stderr
to same output (truncating):
cmd > out.txt 2>&1
“Redirection statements are evaluated, as always, from left to right. >> file
- STDOUT
to file (append mode) (short for 1>> file
) 2>&1
- STDERR
to ‘where stdout goes’ Note that the interpretation ‘redirect STDERR
to STDOUT
’ is wrong”