How to redirect Unix/Linux STDOUT and STDERR to the same file/location

Unix/Linux redirection FAQ: How do I redirect Unix STDOUT and STDERR to the same file with one command?

To redirect both STDOUT and STDERR to the same file with one Unix/Linux command, use this syntax:

my-shell-script.sh > /dev/null 2>&1

As you can see in this command, I'm redirecting STDOUT to /dev/null as normal, and then the special 2>&1 syntax tells your Bash shell to redirect STDERR to STDOUT (which is already pointing to /dev/null).

You don't have to redirect your output to /dev/null, you can redirect it to a log file if you need to, something like this:

my-script.sh > logfile 2>&1

I use this technique to redirect STDOUT and STDERR in many Unix shell scripts and crontab entries, and occasionally from the Unix command line, as I just did now to create a combined STDOUT/STDERR log file to debug a problem I'm having.

Redirect STDOUT/STDERR at the same time - Summary

I hope this tip on how to redirect Unix/Linux STDOUT and STDERR to the same file has been helpful. As usual, if you have any questions or comments, leave a note in the Comments section below.