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

1. What You Will Learn


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:

BASH
# 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
TEXT

          ┌──────────┐
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.

💡 Tip: /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.

TEXT
# 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

BASH
# 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:

TEXT
Desktop
Documents
    Downloads
A new line
Another line

(5) ▶ Example: Separate Normal Output from Error Output

BASH
# 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

BASH
# 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

BASH
# 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

BASH
# 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:

TEXT
server {
    listen 80;
    server_name example.com;
    root /var/www/html;
}

(9) ▶ Comprehensive Example: Output Management During Program Compilation

BASH
# 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>&1 mean? A: \1

Q: Why use &> instead of 2>&1? A: &> is a bash 4+ shorthand that's more concise. 2>&1 is more portable (works in sh as well). Both do the same thing.

Q: Is /dev/null a vacuum? Can it fill up? A: No. Data written to /dev/null is discarded by the kernel directly, using zero disk space. It's essentially a "black hole with infinite capacity."

Q: Must EOF in heredoc be uppercase? A: No. EOF is just a convention; you can use any string (EOL, END, __EOF__). The key is that the start and end markers must match exactly.


📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Save ls -la output to home-list.txt, then append the current date with >>
  2. Intermediate (Difficulty ⭐⭐): Run find / -name "*.py" 2> errors.txt to view permission denied errors, then use &> to capture both stdout and stderr in one file, and use /dev/null to suppress error output
  3. 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
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏