How to Redirect stderr to stdout in Bash

When you redirect the output of a command to a file or pipe it to another command

2 min read
By myfreax
How to Redirect stderr to stdout in Bash
How to Redirect stderr to stdout in Bash

When you redirect the output of a command to a file or pipe it to another command, you may notice that an error message has been printed on the screen.

In this tutorial, we will explain what stdout, stdin, and stderr are.

How to use the redirection operator >, redirect standard output, to redirect standard errors to standard output, so that all output of a program is saved to a file.

In Bash and other Linux shells, when a program is executed, it uses three standard I/O streams. Each I/O stream is represented by a digital file descriptor.

0 indicates the stdin standard input stream. 1 indicates the stdout standard output stream. 2 indicates the stderr standard error stream.

File descriptors are simply numeric descriptors that represent open files.

Standard input is usually keyboard input. The output of the program goes into the standard output stream, and the error message goes into the standard error stream.

By default, both input streams and error streams are printed on the screen.

Redirecting stdout

Redirection is a way to capture output from a program and input it as another program or file.

Redirection can be redirected using the n> operator, where n is the file descriptor. When n is omitted, the default is standard output stream 1.

For example, the following two commands are the same. Both will redirect the standard output of command to a file. To redirect the standard error stream stderr, use the 2> operator.

command > file # eq command 1> file


command 2> file #redirect the standard error stream stderr to file

You can write both standard errors and standard output to two separate files by running command 2> error.txt 1> output.txt.

To disallow the display of error messages on the screen, redirect the standard error stream stderr to the /dev/null null device.

command 2> error.txt 1> output.txt

command 2> /dev/null 

Redirecting stderr to stdout

When you save the output of a program to a file, it is common to redirect standard errors to standard output so that everything is saved in one file.

command > file 2>&1

> file redirects command's standard output to file, and 2>&1 redirects command's standard error to standard output.

Let's see how we can test this. For example, we want to verify that the abcdefg command exists on our system. Run the command abcdefg.

command abcdefg displays an error message on the terminal. command not found: The abcdefg command is not found.

When you run command abcdefg > file 2>&1, the terminal does not print any message, and you can find the error message in file.

command abcdefg

command abcdefg > file

command abcdefg > file 2>&1

The order in which you redirect is also important. For example, the following example simply redirects standard output to a file. This is because standard errors are redirected to standard output.

Only standard output redirects to the file, so you'll also see an error message printed on the terminal.

Another way to redirect standard errors to standard output is to use the &> construct. In Bash, &> has the same meaning as 2>&1.

command 2>&1 > file

command &> file

conclusion

When working on the command line, it is important to understand the concepts of redirections and file descriptors. To redirect stderr and stdout, use the 2>&1 or &> construct.

If you have any questions or feedback, please feel free to comment.