Standard Streams

Understanding stdin, stdout, and stderr standard streams for input/output handling and stream manipulation

Standard streams are three default communication channels between a Linux program and its environment. They provide a standardized way for programs to receive input, display output, and report errors. Understanding streams is fundamental for command-line operations, scripting, and system administration.

Key Concepts

  • stdin (0): Standard input - where programs read data
  • stdout (1): Standard output - where normal output goes
  • stderr (2): Standard error - where error messages go
  • File Descriptors: Numeric identifiers (0, 1, 2) for streams
  • Redirection: Changing where streams point using operators

Command Syntax

Redirection operators work with any command:

  • > - Redirect stdout to file (overwrite)
  • >> - Redirect stdout to file (append)
  • < - Redirect stdin from file
  • 2> - Redirect stderr to file
  • &> - Redirect both stdout and stderr

Common Options

> - Overwrite file with stdout >> - Append stdout to file < - Read stdin from file 2> - Redirect stderr only 2>&1 - Redirect stderr to stdout | - Pipe stdout to next command /dev/null - Discard output (black hole)

Practical Examples

Example 1: Basic Output Redirection

1
2
ls -la > directory_listing.txt
cat directory_listing.txt

Saves directory listing to file instead of displaying on screen

Example 2: Append to File

1
2
echo "New entry" >> logfile.txt
echo "Another entry" >> logfile.txt

Adds lines to existing file without overwriting

Example 3: Input Redirection

1
2
sort < unsorted_names.txt
wc -l < /etc/passwd

Feeds file content as input to commands

Example 4: Error Redirection

1
2
find /etc -name "*.conf" 2> errors.txt
ls nonexistent_file 2>/dev/null

Captures error messages separately from normal output

Example 5: Combine stdout and stderr

1
2
command > output.txt 2>&1
command &> all_output.txt

Both methods redirect all output to same file

Use Cases

  • Logging: Capture command output for later analysis
  • Batch Processing: Feed data from files to commands
  • Error Handling: Separate normal output from errors
  • Silent Operations: Suppress unwanted output
  • Automation: Redirect streams in scripts and cron jobs

tee - Write output to file AND display on screen cat - Display file contents or concatenate head/tail - Show beginning/end of streams grep - Filter stream content sort - Sort input stream wc - Count lines, words, characters in stream

Tips & Troubleshooting

Common Issues

  • Permission denied: Check write permissions for output files
  • File not found: Verify input file paths exist
  • Disk full: Monitor space when redirecting large outputs
  • Overwrites: Use >> instead of > to preserve existing data

Best Practices

  • Always redirect errors in scripts: command 2>/dev/null
  • Use absolute paths in automated scripts
  • Test redirections with small data first
  • Combine with tee when you need both file and screen output

Advanced Techniques

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Redirect different streams to different files
command > output.txt 2> errors.txt

# Use process substitution
diff <(command1) <(command2)

# Here documents for multi-line input
cat << EOF > config.txt
Setting 1
Setting 2
EOF