Quiz Entry - updated: 2026.07.05
How do you redirect both stdout and stderr to the same file?
Send stdout to the file first, then add 2>&1 to copy stderr to the same place: command > file 2>&1.
command > output.log 2>&1 # both streams, overwrite
command >> output.log 2>&1 # both streams, append
Decoding 2>&1: read it as "make FD 2 (stderr) go to wherever FD 1 (stdout) currently points." The &1 is essential — it means "the descriptor numbered 1", not a file literally named 1. Without the &, 2>1 would create a file called 1.
Why order is non-negotiable — the shell sets up redirects left to right, and 2>&1 captures stdout's target at that instant:
utility > output.log 2>&1 # CORRECT: stdout→file, THEN stderr copies file
utility 2>&1 > output.log # WRONG: stderr→console (stdout's current spot),
# THEN stdout moves to file; errors stay on screen
Tip: Bash's &> file does the same in one token and avoids the trap — but > file 2>&1 is the portable form that works in plain sh.
Go deeper:
Bash Reference: Redirections —
2>&1as descriptor duplication and why stdout must be redirected first.