Tame Your Logs: Unified `stderr` and `stdout` with `2>&1`

Quick Tip

Tame Your Logs: Unified `stderr` and `stdout` with `2>&1`

Challenge: When running commands or scripts, you often want to capture both standard output (stdout) and standard error (stderr) into a single log file. By default, they are directed to separate streams, making unified logging difficult.

The Solution: Use the `2>&1` redirection operator to send stderr to stdout, and then redirect stdout to your desired log file.

your_command > output.log 2>&1

Why it works: In bash, file descriptor 1 represents stdout and file descriptor 2 represents stderr. The `2>&1` tells the shell to redirect file descriptor 2 (stderr) to the current location of file descriptor 1 (stdout). The `>` then redirects the combined output of both streams to `output.log`.

Pro-Tip: For appending to a log file instead of overwriting it, use `>>` instead of `>`: your_command >> output.log 2>&1

Linux Tips & Tricks | © ngelinux.com | 5/29/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments