How to Redirect Stdout and Stderr to File in Bash [5 Cases]

While working with Bash scripts, sometimes it’s crucial to manage both the standard output (stdout) and standard error (stderr) effectively. By default, stdout displays the regular output of a command, while stderr displays error messages. In this writing, I will discuss how you can redirect stdout and stderr to file using the Bash redirection process with some practical Bash script examples.

Key Takeaways

  • Learning the importance of Bash Stdout & Stderr Redirection.
  • Getting a practical overview of the process with Bash script examples.

Free Downloads

Importance of Redirecting Stdout and Stderr in Bash

In Linux, every process has two default output streams, stdout (represented by file descriptor 1) & standard error or stderr (represented by file descriptor 2). You can redirect both of them using the symbol ‘&> or ‘>&’ or ‘2>&1’. Now redirecting stdout & stderr in Bash is essential for several reasons:

  • Separating Normal Output from Errors: By redirecting ‘stdout’ and ‘stderr’ to different files or locations one can clearly distinguish between them.
  • Preventing Unwanted Output: Sometimes, programs generate debug messages on ‘stderr’ that you don’t want to display which can be discarded by sending them to the null device.
  • Pipeline Management: When you use pipes (|) to pass the output of one command as input to another, it’s important to ensure that only intended data is being transferred, which can be done by redirecting Stderr.
  • Error Handling: Redirecting ‘stderr’ to a file allows you to check for errors and take appropriate actions based on the error messages received.
  • Automation: In automated processes, scripts often run without user interaction. Redirecting ‘stdout’ and ‘stderr’ to files ensures that all relevant information is captured for reviewing later.

5 Practical Cases of Bash Redirect Stdout and Stderr to File

In the following article, I will show how you can redirect both command output & error messages in different practical scenarios while generating Bash scripts.

Case 1: Redirecting Both Stdout and Stderr to the Same File in Bash

In Bash, you can redirect both stdout & stderr to the same file using the ‘&>’ redirection operator. Follow the below steps to see how I used the redirection process while generating a Bash script:

Steps to Follow >

❶ At first, open your Ubuntu Terminal application.

❷ Now, open a file, let’s say, named ‘same_file.sh’ in the nano editor by using the following command:

nano same_file.sh

➌ Next, write the following script inside the Bash file in the nano text editor:

#! /bin/bash

cd /nonexistent_directory &> log.txt
cat log.txt

EXPLANATION
In the script’s first line, ‘#!’ is shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s Bash situated in the /bin directory. In the next line, the output & error messages of the ‘cd command’ are redirected to a file named ‘log.txt’ using the ‘>’ operator. If ‘log.txt’ already exists, it will be overwritten with the new output. Otherwise, a new fill will be created. Later, the contents of that file will be displayed using the ‘cat command.

➍ Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor.

➎ After that, use the following command to make the script executable:

chmod u+x same_file.sh

explanation
  • chmod: Changes the permission of files and directories.
  • u+x: Argument with chmod command to add the executable permission for the user.
  • same_file.sh: File which you want to make executable.

➏ Finally, run the script by the following command:

./same_file.sh

Redirect stdout and stderr to the same file in bashThe output & output error messages both are redirected to the ‘log.txt’ file and I viewed the contents of the ‘log.txt’ file using the cat command in the script.

Case 2: Redirecting Stdout to a File and Stderr to a Different File in Bash

If you want you can redirect both stdout & stderr but to separate files too. For that check out the following script:

You can follow the Steps of Case 01, to save & make the script executable.

Script (diff_file.sh) >

#! /bin/bash

{
 whoami
 whoami -l
} > output.txt 2> error.txt

echo "The output is '$(cat output.txt)'"
echo "The error is '$(cat error.txt)'"

EXPLANATION
In this script, the curly braces ‘{…}’ are used to group the two whoami commands. Both of them are executed, with their stdout redirected (>) to the ‘output.txt’ file & stderr (if any) generated by them will be redirected (2>) to the ‘error.txt’ file. Later I viewed the file contents with the help of the cat command while echoing messages (‘The output is’ & ‘The error is’) using the echo command.

Now, run the script by the following command:

./diff_file.sh

Redirect stdout & stderr to different file in bashFrom the image, you can see the redirected stdout & stderr of the commands inside the two different files.

Case 3: Appending Both Stdout and Stderr to a File in Bash

You can append both stdout and stderr instead of just redirecting at the same time to the same file. This way the data will be appended to the specified file instead of overwriting it. For that purpose, use the append symbol ‘>>’. Check out the following script:

You can follow the Steps of Case 01, to save & make the script executable.

Script (appending.sh) >

#! /bin/bash

cat output.txt
whoami >> output.txt 2>&1
cat output.txt

EXPLANATION
The script appends the output & error messages of the whoami command to the output.txt files. Later, the cat command displays the new contents of the output.txt file after appending.

Now, run the script by the following command:

./appending.sh

Appending stdout & stderr in bashSee from the image, the new output (munny) is appended with the previous file contents of the ‘output.txt’ file.

Case 4: Displaying Stdout on the Terminal and Redirecting Stderr to a File in Bash

Sometimes you may need to display just the stdout of certain commands, not the error messages for a cleaner terminal. You can do that just by displaying the stdout & redirecting the stderr to a file. See the below script for a practical approach:

You can follow the Steps of Case 01, to save & make the script executable.

Script (display.sh) >

#! /bin/bash

{
  date
  date -r /var/empty/foo
} 2> error.txt

EXPLANATION
In this script, the curly braces ‘{…}’ are used to group the two date commands. Both of them are executed, with their stdout displayed on the terminal. But the ‘2> error.txt’ part ensures that any error messages generated by either of the date commands will be redirected to the error.txt file.

Now, run the script by the following command:

./display.sh

Displaying stdout & stderr to a file You can see the script only displays the stout of both commands, not the stderr. Later I viewed the error.txt file for viewing the error messages using the cat command.

Case 5: Discarding Stdout and Displaying Stderr on the Terminal

While working in Bash scripts, you may need to suppress certain command output, as you need only the error messages for debugging purposes. So instead of redirecting Stdout inside a file, you can just discard it.

Now the simplest and most common approach to suppress any output in Bash is by redirecting it to the null device, ‘/dev/null’. This special file in Unix-like systems discards all data written to it.

You can follow the Steps of Case 01, to save & make the script executable.

Script (null.sh) >

#! /bin/bash

{
  whoami
  date
  date -r /var/empty/foo
} > /dev/null

EXPLANATION
In this script, the entire block of commands (whoami, date, date -r /var/empty/foo) within the curly braces ‘{…}’ is grouped & their combined output will be redirected (‘>’) to ‘dev/null’. This means the output messages will be discarded. But the generated stderr (if any) of these commands will still be displayed on the terminal.

Now, run the script by the following command:

./null.sh

Discarding stdout & displaying stderr in bashFrom the output image, you can see the script only displaying the error messages.

Conclusion

To sum up, whether you want to store, append, display, or discard the stdout and stderr of commands, the Bash redirection process helps to manage them. The discussed practical cases in this article should help you to learn how to handle them. Happy Bash scripting!

People Also Ask

Which is used to redirect stdout and stderr to a file?

To redirect stdout and stderr to the same file you can use the redirection operator ‘&>’. For that use the command syntax, ‘command &> output.txt’. Here, ‘command’ is your desired command, of which you want to see the output & error information & ‘output.txt’ is the file name to which both stdout & stderr will be redirected.

How to redirect both standard output and standard error to the same location?

You can redirect both standard output and standard error to the same location using the command syntax, ‘command &> output.txt’. Here, ‘command’ is your desired command, of which you want to see the output & error information & ‘output.txt’ is the file name to which both stdout & stderr will be redirected.

Which output redirection operator send output to both the screen and a file at the same time?

To redirect the output of a command both to the terminal & to a file you can use the ‘tee command’ along with the pipe redirection operator (|). For example, if you want to view the ‘ls command’ output to your terminal screen & also want to redirect the output to a file named ‘output.txt’, then use the command syntax, ‘ls | tee output.txt’.

Which character is used to redirect output to a file?

The redirection operator ‘>’ is used to redirect output to a file. For example, if you want to redirect the ‘ls command’ output to a file named ‘output.txt’, then use the command syntax, ‘ls > output.txt’.

Rate this post
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment