Linux: Standard Streams and Redirection
Linux treats every program as a "filter" — receiving input from somewhere, processing it, and outputting results. The channels for input and output are the standard streams.
📋 Prerequisites: You should first complete
- Lesson 5: File and Directory Operations
1. What You Will Learn
- Concepts of standard input, output, and error
- The difference between
>and>> 2>for redirecting errors&>for redirecting both streams- Applications of
/dev/null
2. A Screen Full of Errors Story
(1) The Pain: Mixed Output Makes It Hard to Find What You Need
Alex ran apt update, and success messages mixed with red error messages scrolled across the screen. He only wanted to save the successful results and look at the errors separately.
(2) Splitting Output with Redirection
Bob taught him to use redirection:
# Save success messages to success.log, errors to error.log
sudo apt update > success.log 2> error.log
# View the errors
cat error.log
(3) The Benefit: Precise Output Control
Alex learned to route different outputs to separate streams — log files only record successes, the terminal only shows important information, and errors are saved separately for troubleshooting.
3. Knowledge Points
(1) The Three Standard Streams
When a Linux program starts, it automatically receives three file descriptors:
| File descriptor | Name | Default connection | Purpose |
|---|---|---|---|
| 0 | stdin | Keyboard | Program input |
| 1 | stdout | Terminal screen | Normal program output |
| 2 | stderr | Terminal screen | Program error output |
┌──────────┐
stdin ──→│ │──→ stdout
(keyboard)│ Program │ (screen)
│ │──→ stderr
└──────────┘ (screen)
(2) Redirection Operators
| Operator | Meaning | Example |
|---|---|---|
> |
Write stdout to file (overwrite) | ls > files.txt |
>> |
Append stdout to file | echo "done" >> log.txt |
< |
Read stdin from file | sort < list.txt |
2> |
Write stderr to file | cmd 2> error.log |
2>> |
Append stderr to file | cmd 2>> error.log |
&> |
Write both stdout and stderr to file | cmd &> all.log |
&>> |
Append both stdout and stderr to file | cmd &>> all.log |
2>&1 |
Merge stderr into stdout | cmd > log.txt 2>&1 |
ℹ️ Note:
>overwrites (existing file content is erased), while>>appends (adds to the end of the file). Use>>when you need to preserve historical logs; use>when you need a clean state. Misusing>can overwrite important log files.
(3) /dev/null — The Black Hole
/dev/null is a special device file — any data written to it is discarded, and reading from it returns nothing.
/dev/null is Linux's "black hole device" — the kernel directly discards written data at the driver level, using no disk space, and it never fills up. Commonly used to silence command output: command &> /dev/null completely suppresses all output, command 2> /dev/null only suppresses errors.
# Discard all output
command > /dev/null 2>&1
# Or the shorter form
command &> /dev/null
# Only discard errors
command 2> /dev/null
# Only discard normal output
command > /dev/null
(4) ▶ Example: Save Command Output to a File
# Overwrite write
ls ~ > home-directory.txt
cat home-directory.txt
# Append write
echo "A new line" >> home-directory.txt
echo "Another line" >> home-directory.txt
cat home-directory.txt
Output:
TEXTDesktop Documents Downloads A new line Another line
(5) ▶ Example: Separate Normal Output from Error Output
# Save success messages, view errors separately
find /etc -name "*.conf" > found.txt 2> errors.txt
# View results
echo "=== Found config files ==="
head -5 found.txt
echo "=== Error messages ==="
cat errors.txt
Output:
TEXT=== Found config files === /etc/adduser.conf /etc/ca-certificates.conf /etc/debconf.conf /etc/deluser.conf /etc/fuse.conf === Error messages === find: '/etc/ssl/private': Permission denied
(6) ▶ Example: Merge Output
# Method 1: &> (bash 4+)
grep -r "error" /var/log/ &> all-results.txt
# Method 2: 2>&1 (more portable)
grep -r "error" /var/log/ > all-results.txt 2>&1
# Both methods produce the same result — stdout and stderr go to the same file
> 💡 Tip: The order of `2>&1` matters! `cmd > log.txt 2>&1` is correct — first redirect stdout to a file, then merge stderr into stdout (which now points to the file). If written as `cmd 2>&1 > log.txt`, stderr would first merge into the screen's stdout, and then stdout would redirect to the file, leaving stderr still going to the screen.
Output:
TEXT$ wc -l all-results.txt 42 all-results.txt
(7) ▶ Example: /dev/null to Suppress Output
# Completely silent execution
sudo apt update > /dev/null 2>&1
# Shorter form
sudo apt update &> /dev/null
# Only suppress errors
find / -name "*.py" 2> /dev/null
Output:
TEXT(No output — all errors and normal results suppressed)
(8) ▶ Example: heredoc — Embed Multi-line Text in Scripts
# Create a multi-line configuration file
cat << EOF > nginx.conf
server {
listen 80;
server_name example.com;
root /var/www/html;
}
EOF
# Append multi-line text
cat << EOF >> ~/.bashrc
# Add PATH
export PATH="\$PATH:\$HOME/scripts"
EOF
# Variables in heredoc won't be expanded (use quoted 'EOF')
cat << 'EOF' > greeting.sh
#!/bin/bash
echo "Hello, $USER" # $USER remains as variable name
EOF
Output:
TEXTserver { listen 80; server_name example.com; root /var/www/html; }
(9) ▶ Comprehensive Example: Output Management During Program Compilation
# Compile a C program, logging output and errors separately
gcc main.c -o app \
> build.log \ # Compilation success messages
2> build-errors.log # Compilation error messages
# If compilation succeeds, build-errors.log is empty
# If compilation fails, view the errors
if [ -s build-errors.log ]; then
echo "❌ Compilation failed, errors:"
cat build-errors.log
else
echo "✅ Compilation successful!"
fi
❓ FAQ
Q: What's the difference between
>and|(pipe)? A: \1
Q: What does
2>&1mean? A: \1
Q: Why use
&>instead of2>&1? A:&>is a bash 4+ shorthand that's more concise.2>&1is more portable (works in sh as well). Both do the same thing.
Q: Is
/dev/nulla vacuum? Can it fill up? A: No. Data written to/dev/nullis discarded by the kernel directly, using zero disk space. It's essentially a "black hole with infinite capacity."
Q: Must
EOFin heredoc be uppercase? A: No.EOFis just a convention; you can use any string (EOL,END,__EOF__). The key is that the start and end markers must match exactly.
📖 Summary
- Standard input (stdin/0) → Program → Standard output (stdout/1) + Standard error (stderr/2)
>overwrites,>>appends,2>redirects errors&>merges output,/dev/nulldiscards output2>&1merges stderr into stdout (mind the order)- heredoc
<< EOF ... EOFembeds multi-line text in scripts
📝 Exercises
- Basic (Difficulty ⭐): Save
ls -laoutput tohome-list.txt, then append the current date with>> - Intermediate (Difficulty ⭐⭐): Run
find / -name "*.py" 2> errors.txtto view permission denied errors, then use&>to capture both stdout and stderr in one file, and use/dev/nullto suppress error output - Advanced (Difficulty ⭐⭐⭐): Use heredoc to create a configuration file with 5 lines of content, then write a script demonstrating all four redirection methods (
>,2>,&>, and>>) and explain when each is appropriate