Redirect `STDERR` to `STDOUT` 2>&1

Any command executed in Bash or any other Linux shell, has one input stream (stdin) and two kinds of output streams: standard output (stdout) and standard error (stderr).

Each of these streams is represented by a numeric file descriptor:

  • 0stdin, the standard input stream
  • 1stdout, the standard output stream
  • 2stderr, the standard error stream

There is an advantage in separating the stdout and stderr streams, as it allows to redirect/pipe a command’s output (stdout) to a file or anther command and still see any error messages (stderr) in the terminal.

But sometimes it is required to redirect the stderr stream to stdout or redirect the both stderr and stdout to a file or to /dev/null and in this note i am showing how to do this.

Redirect stderr to stdout

Redirect stderr to stdout:

$ <command> 2>&1

Redirect stderr and stdout to a file:

$ <command> 2>&1 > <file>

Redirect stderr and stdout to /dev/null:

$ <command> 2>&1 > /dev/null

Leave a Reply