How to Redirect stdout and stderr in Bash (Complete Guide)

Redirecting output streams is a core skill for any Linux or Unix user. Whether you are debugging scripts, monitoring long-running processes, or automating server tasks, knowing how to redirect stdout and stderr is essential. In this guide, we’ll cover everything from basic redirection to advanced logging techniques, with practical examples you can try immediately.


Understanding stdout and stderr

In Linux, commands write output to two main streams:

  • stdout (standard output) – normal messages from commands.
  • stderr (standard error) – error messages and warnings.

By default, both are printed on the terminal, but in real-life scenarios, you often want to save output to files, send it to multiple destinations, or pipe it to another command.

Example:

ls /nonexistent

Generates an error on stderr.


Basic stdout redirection

Redirect stdout to a file:

echo "Hello Linux" > output.txt
  • > overwrites the file.
  • Append instead of overwrite:
echo "Another line" >> output.txt

Multiple commands with stdout

echo "Step 1 completed" >> output.txt
echo "Step 2 completed" >> output.txt

Example: logging multiple script steps:

./deploy_script.sh >> deploy.log

Redirect stderr to a file

To capture errors:

ls /nonexistent 2> error.log
  • 2> redirects stderr to a file.
  • Append instead of overwrite:
ls /nonexistent 2>> error.log

Practical scenario: capturing errors from multiple commands:

mkdir /restricted 2>> error.log
cp /etc/shadow /backup/ 2>> error.log

Redirect stderr to stdout

Combine stdout and stderr in one file:

command 2>&1 > combined.log

Example: compiling a C program:

gcc program.c 2>&1 | tee compile.log
  • Both errors and normal messages are saved and displayed.
  • This method is ideal when running long scripts or large builds.

Using tee to redirect stdout to multiple files

tee writes stdout to multiple files and to the terminal.

Example:

echo "Logging this message" | tee file1 file2
  • Writes to file1 and file2.
  • Prints to terminal.

Append mode:

echo "Another line" | tee -a file1 file2

Monitoring scripts in real time:

./long_script.sh | tee script_output.log

Using vim split screen, you can monitor logs in one pane while editing scripts in another.


Redirecting both stdout and stderr to multiple files

Sometimes you want both stdout and stderr saved in multiple files. Combine 2>&1 with tee:

somecommand 2>&1 | tee file1 file2

Example: logging a server update:

apt update 2>&1 | tee update.log backup.log
  • All output, including errors, is logged.
  • Great for auditing or troubleshooting automated processes.

Separate logging for stdout and stderr

Advanced users often want separate files:

somecommand > >(tee stdout.log) 2> >(tee stderr.log >&2)

Explanation:

  • > redirects stdout, 2> redirects stderr.
  • Each stream is logged separately.
  • >&2 ensures stderr behaves correctly.

Example: database migration:

pg_dump mydb > >(tee stdout.log) 2> >(tee error.log >&2)
  • Successful operations go to stdout.log.
  • Errors go to error.log.

Piping stdout and stderr

Sometimes you want to pipe output to another command, for filtering or processing:

ls -l /nonexistent 2>&1 | grep "No such file"
  • 2>&1 merges stderr into stdout so grep sees errors too.

Example: monitoring disk usage errors:

df -h 2>&1 | grep "Permission denied"

Copying files to multiple destinations

tee can duplicate files, useful for backups:

cat /etc/passwd | tee /home/nobody/pwbackup /root/pwbackup
  • Saves time by copying to multiple locations in one command.
  • Running as nobody user avoids accidental root-level changes.

Example: distribute config files:

cat config.conf | tee ~/user1/config.conf ~/user2/config.conf ~/user3/config.conf

Combining stdout and stderr with append

Append outputs instead of overwriting:

command >> all_output.log 2>&1

Example: long-running script logging:

./backup.sh >> backup.log 2>&1
  • Existing logs are preserved.
  • Errors and messages are combined.

Redirecting stdout and stderr in scripts

You can combine multiple commands in a script and redirect outputs cleanly:

{
echo "Starting process"
mkdir /restricted
cp /etc/shadow /backup/
echo "Process finished"
} >> script.log 2>&1
  • { } groups commands.
  • >> script.log appends stdout.
  • 2>&1 ensures errors are also logged.

Real-world examples

  1. Logging system update:
apt update 2>&1 | tee -a update.log
apt upgrade 2>&1 | tee -a update.log
  1. Monitoring disk usage and logging errors:
df -h 2> disk_errors.log | tee disk_usage.log
  1. Compiling multiple projects:
for dir in project1 project2; do
cd $dir
make 2>&1 | tee -a build.log
cd ..
done
  1. Automated backup script:
rsync -av /data /backup 2>&1 | tee -a backup.log

Tips for Linux admins

  • Always identify which stream you need to redirect: stdout or stderr.
  • Use tee -a to avoid overwriting logs.
  • For advanced scripts, combine tee with process substitutions.
  • Run test scripts under the nobody user for safety.
  • Use heredoc in Bash to feed multiple commands into a single file for logging.

These techniques are especially useful when editing scripts in Vim, monitoring multiple outputs simultaneously, or managing system logs.


Summary

Mastering stdout and stderr redirection allows you to:

  • Monitor scripts in real time
  • Capture errors for debugging
  • Automate Linux administration tasks
  • Maintain organized logs for auditing

Combining tee with Bash redirection operators gives you full control over your command-line operations, making your Linux workflows more efficient and reliable.

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *