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:
❶ 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
➍ 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
- 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
The 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:
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)'"
Now, run the script by the following command:
./diff_file.sh
From 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:
Script (appending.sh) >
#! /bin/bash
cat output.txt
whoami >> output.txt 2>&1
cat output.txt
Now, run the script by the following command:
./appending.sh
See 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:
Script (display.sh) >
#! /bin/bash
{
date
date -r /var/empty/foo
} 2> error.txt
Now, run the script by the following command:
./display.sh
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.
Script (null.sh) >
#! /bin/bash
{
whoami
date
date -r /var/empty/foo
} > /dev/null
Now, run the script by the following command:
./null.sh
From 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!