Redirecting Bash output to a file

Say you have a bash shell script and you want to have the output (stdout) and the error message (stderr) of a command written to a file. Here is how you should do it. I also found some useful information here.

$ mycommand 1> outputfile.txt # Writes stdout to outputfile.txt
$ mycommand > outputfile.txt # Same as above
$ mycommand >> outputfile.txt # Writes stdout to outputanderrorfile.txt but appends instead of overwriting
$ mycommand 2> errorfile.txt # Writes stderr to errorfile.txt
$ mycommand 2>> errorfile.txt # Writes stdout to errorfile.txt but appends instead of overwriting
$ mycommand > outputanderrorfile.txt 2>&1 # Writes stdout and stderr to outputanderrorfile.txt
$ mycommand &> outputanderrorfile.txt # Writes stdout and stderr to outputanderrorfile.txt
$ mycommand >> outputanderrorfile.txt 2>&1 # Writes stdout and stderr to outputanderrorfile.txt but appends instead of overwriting

Comments

Popular Posts