How to Redirect stderr and stdout in Bash?

The redirect operator in Linux is used to redirect the input/output streams to the file in bash. Every shell in Linux, such as Bash, K-shell, C-shell, and others, uses three input/output streams to execute a command. The system takes the standard input, which can display two messages; if the input is successfully executed, it generates standard output (stdout). The standard error (stderr) message is displayed if an error occurs.

This article will explain the methods to redirect stderr and stdout with the following supporting content:

Let’s start with the types of I/O streams available in bash.

What are I/O Streams for Redirect Operators in Bash?

These three standard I/O streams are associated with a specific numeric descriptor written below:

Standard input (stdin): 0

The stdin receives commands from the user.

Standard output (stdout): 1

A terminal displays the output from stdout.

Standard error (stderr): 2

The stderr output stream shows the error messages on the output.

Sometimes, we need to store the stdout and stderr messages to a file performed using the redirect operator (>). While redirecting the I/O stream, we need the “numeric descriptor” with the redirect operator. For instance, to redirect the output in a file “1>” is used, while “2>” is used in case of error. The default numeric descriptor is “1”.

Note: The single redirect operator (>) deletes the previous data and redirects the current data only. While the double redirect operators (>>) do not delete the previous data in the file but append the new data to the file.

The following methods are used to redirect the stderr and stdout in Bash:

Commands > output.txt Commands 1> output.txtAny of both commands redirects the output of the file to the output.txt file.
Commands >> output.txt Commands 1>> output.txtBoth commands append the data into the output.txt file.
Commands 2> error.txtThe error output stderr is redirected to the error.txt file.
Commands 2>> error.txtAppends the error messages to the error.txt file.
Commands > output.txt 2>&1The output and error are redirected to the output.txt file.
Commands >> output.txt 2>&1It appends output stdout and the error stderr to the output.txt file.
Commands 2>error.txt 1>output.txtThis command redirects the standard error stderr to the “error.txt” file, while the standard output stdout is redirected to a separate file output.txt. 
Commands 2>&- Commands 2> /dev/nullBoth commands do not show the error messages or do not store them in any file but suppress them. These command does not show any error messages on the display screen.

How to Redirect stderr and stdout in Bash?

To redirect stderr and stdout in Bash, we can utilize the &> or >& operator to place the output of a command to a file. This section will use different methods to redirect stderr and stdout in Bash.

Example 1: Redirect stdout to a Particular File in Bash

The default behavior of the redirect operator (>) is to redirect the output into a file. We can redirect the command output into a file as well. For instance, to redirect the output of the “ls” command into a file “output.txt”, the following command is used: 

#!/bin/bash echo "My Files are: $(ls)" > output.txt

To run the bash script and check its output (cat), the following commands are used:

$ bash script.sh 
$ cat output.txt

The output shows the “ls” command result is redirected to the “output.txt” file.

When you use the two redirect operators (>>) together, it will append the data in the desired. For example, to append the “USERNAME” command’s output to the output.txt file, this command is utilized:

#!/bin/bash
echo "New output: ($USERNAME)" >> output.txt

Tor execute the bash file and view the output of the “output.txt”, use below commands:

$ bash script.sh
$ cat output.txt

The bash output is appended at the end of the “output.txt” file.

Example 2: Redirect stderr to a Particular File in Bash

We can redirect the output to a specific file using the numeric error descriptor with the redirect operator “2>”. For instance, we can are the directory to a non-existent directory without sudo permissions, which will show the error, which will be redirected to the “error.txt” file:

#!/bin/bash
cd /home/itslinuxfoss/Hello 2> error.txt

Now, check the error.txt file where error messages are redirected using the following cat command:

$ bash script.sh 
$ cat error.txt

The “cat” command displays that the error message is redirected to the “error.txt” file.

Example 3: Redirect stdout and stderr Combined to a Particular File in Bash

We can redirect the output (stdout) and error message (stderr) to the same file using the “2>&1” operator, which will redirect the error message to the standard output and will display both outputs at default standard output. For instance, use the below command to send the output and error to the same file: 

#!/bin/bash
(ls -l && cd ~/Hello) > both.txt 2>&1

Execute the script and check the output using the following commands:

$ bash script.sh 
$ cat both.txt

The output shows the successful execution output (stdout) for the ls command, and the error for the “cd” command is also redirected to a similar file.

Example 4: Redirect stdout and stderr to Separate Files in Bash

Sometimes, we need to send the output of the error message (stderr) to its own file while the standard output (stdout) to a different file using the redirect operator. To send the error to a file named “error.txt” while output to “output.txt”, use the following command:  

#!/bin/bash
(ls -l && cd ~/Hello) 2> error.txt 1> output.txt

Run the bash script and display the both file’s content, using blow commands:

$ bash script.sh
$ cat error.txt
$ cat output.txt

The output verifies that the error message is redirected to the “error.txt” file while the output is redirected to the “output.txt” file.

Example 5: Suppress the Error Messages stderr in Bash

If you do not want to show the error message on the terminal, two approaches can be used. For instance, to suppress the error message from showing on the terminal, use this command: 

#!/bin/bash
cd /home/itslinuxfoss/Hello 2>&-

Now, execute the script to check if the error message display on the terminal:

$ bash script.sh

The error message is not showing on the terminal.

Another approach to suppress the error message is to redirect the error message to the “/dev/null” file. For instance, to suppress the error message (of cd command), use the following command:

#!/bin/bash
cd /home/itslinuxfoss/Hello 2> /dev/null

Let’s run the bash script code by utilizing the below command:

$ bash script.sh

The standard error (stderr) is not showing on the output.

That’s how to redirect stderr and stdout in Bash.

Conclusion

The redirect operator is used with its numeric descriptor (1 for stdout and 2 for stderr) to redirect the standard error (stderr) and standard output (stdout) to a file in bash. We can redirect the standard output (stdout) to a specific file, standard error (stderr) to a particular file, or both stderr and stdout to a similar file. Moreover, we can suppress the standard error message in bash as performed in this article.