Tame Your Terminal: Redirect `stderr` with `2>&1`

Quick Tip

Tame Your Terminal: Redirect `stderr` with `2>&1`

Challenge: You’re running a command or script, and you want to capture both its standard output (stdout) and standard error (stderr) into a single log file, or pipe them to another command for unified processing.

The Solution: Use the `2>&1` redirection operator.

your_command &> output.log # Or for more granular control: your_command > output.log 2>&1

Why it works: In the shell, file descriptor 1 represents stdout and file descriptor 2 represents stderr. The `2>&1` syntax tells the shell to redirect file descriptor 2 (stderr) to wherever file descriptor 1 (stdout) is currently pointing. When combined with `&>`, it creates a single stream for both.

Pro-Tip: If you only want to discard stderr, you can redirect it to `/dev/null`: your_command 2>/dev/null.

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments