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.
In Linux, every command can use standard streams.
| Stream | Name | Meaning |
|---|---|---|
0 | stdin | Standard input |
1 | stdout | Standard output |
2 | stderr | Standard 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
To save normal output to a file, use >.
ls /home > output.txt
This saves normal command output into output.txt.
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.
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.
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.
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.
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
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.
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.
Apache is one of the most widely used open-source web servers in the world. It is…
Swap space is an area on disk that Linux uses when it runs out of physical…
Zoom is one of the most widely used video conferencing platforms. Zoom works on Windows, macOS,…
Webmin is an open-source web-based control panel for Linux servers. It gives you a browser interface…
MariaDB is an open-source relational database management system. It was created by the original MySQL developers…
Corruption investigations need accuracy, patience, and strong evidence. In 2026, OSINT tools can help researchers,…