How to Redirect stderr to stdout in Bash
3 min read
When redirecting the output of a command to a file or piping it to another command, you might notice that the error messages are printed on the screen.
In Bash and other Linux shells, when a program is executed, it uses three standard I/O streams. Each stream is represented by a numeric file descriptor:
stdin, the standard input stream.
stdout, the standard output stream.
stderr, the standard error stream.
A file descriptor is just a number representing an open file.
The input stream provides information to the program, generally by typing in the keyboard.
The program output goes to the standard input stream and the error messages goes to the standard error stream. By default, both input and error streams are printed on the screen.
Redirection is a way to capture the output from a program and send it as input to another program or file.
Streams can be redirected using the
n> operator, where
n is the file descriptor number.
n is omitted, it defaults to
1, the standard output stream. For example, the following two commands are the same; both will redirect the command output (
stdout) to the file.
command > file
command 1> file
To redirect the standard error (
stderr) use the
command 2> file
You can write both
stdout to two separate files:
command 2> error.txt 1> output.txt
To suppress the error messages from being displayed on the screen, redirect
command 2> /dev/null
When saving the program’s output to a file, it is quite common to redirect
stdout so that you can have everything in a single file.
stdout and have error messages sent to the same file as standard output, use the following:
command > file 2>&1
> file redirect the
2>&1 redirect the
stderr to the current location of
The order of redirection is important. For example, the following example redirects only
file. This happens because the
stderr is redirected to
stdout before the
stdout was redirected to
command 2>&1 > file
Another way to redirect
stdout is to use the
&> construct. In Bash
&> has the same meaning as
command &> file
Understanding the concept of redirections and file descriptors is very important when working on the command line.
stdout, use the
If you have any questions or feedback, feel free to leave a comment.