How To

How To Redirect stderr To stdout In Bash

Introduction

Redirecting stderr to stdout is an important concept in Bash scripting. In Linux, commands usually produce two types of output: normal output and error output. Normal output is called stdout, while error output is called stderr.

If you are learning Bash scripting, understanding output redirection is very useful. It helps you save command output, capture errors, create log files, debug scripts, and monitor automation tasks. This is especially helpful in Linux administration, cybersecurity scripts, backup scripts, and log analysis.

What Are stdout And stderr?

In Linux, every command can use standard streams.

StreamNameMeaning
0stdinStandard input
1stdoutStandard output
2stderrStandard error

For example, when a command works correctly, the result is printed to stdout.

ls /home

If a command fails, the error message is printed to stderr.

ls /wrong-directory

Output:

ls: cannot access '/wrong-directory': No such file or directory

Redirect stdout To A File

To save normal output to a file, use >.

ls /home > output.txt

This saves normal command output into output.txt.

Redirect stderr To A File

To save only error output, use 2>.

ls /wrong-directory 2> error.txt

Now check the file:

cat error.txt

This will show the error message stored inside the file.

Redirect stderr To stdout

To redirect stderr to stdout, use:

command 2>&1

Example:

ls /wrong-directory > output.txt 2>&1

This command sends both normal output and error output to output.txt.

Here, 2 means stderr, 1 means stdout, and 2>&1 means send stderr to the same place as stdout.

Modern Bash Redirection Method

Bash also provides a shorter method:

command &> file.txt

Example:

ls /home /wrong-directory &> result.txt

This saves both successful output and error messages into result.txt.

Append stdout And stderr To A File

To append both stdout and stderr without overwriting the file, use:

command >> file.txt 2>&1

Example:

ls /home /wrong-directory >> log.txt 2>&1

This is useful for log files because it keeps old content and adds new output.

Bash Script Example

Create a script:

nano redirect-example.sh

Add this code:

#!/bin/bashecho "Running directory check..."ls /home /wrong-directory > scan-result.txt 2>&1echo "Result saved to scan-result.txt"

Run it:

chmod +x redirect-example.sh./redirect-example.sh

Cybersecurity Example

You can save scan output and errors together:

nmap 192.168.1.1 > nmap-output.txt 2>&1

This is useful when running scans and saving complete results for later analysis.

Conclusion

Redirecting stderr to stdout in Bash helps you manage normal output and error messages together. You can use 2>&1, &>, and >> file 2>&1 to save command results and errors.

For beginners, this is an important Bash scripting skill for debugging, logging, automation, Linux administration, and cybersecurity reporting.

Cyber Defence

Recent Posts

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

11 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

11 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

12 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

12 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago