Every Linux process has three default streams:
┌─────────────────────────────────────────────────────────────┐
│ Linux Process │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ stdin │ │ stdout │ │ stderr │ │
│ │ (0) │ │ (1) │ │ (2) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ ▲ │ │ │
└─────────┼───────────────────┼───────────────────┼───────────┘
│ │ │
│ ▼ ▼
┌─────────┐ ┌─────────────┐ ┌─────────────┐
│Keyboard │ │ Terminal │ │ Terminal │
│ Input │ │ Screen │ │ Screen │
└─────────┘ │ (normal) │ │ (errors) │
└─────────────┘ └─────────────┘
Example:
# Reading from keyboard (default)
cat # Type something, press Ctrl+D to end
# Reading from file
cat < input.txt # Redirect file to stdin
Example:
# Output to screen (default)
echo "Hello World"
# Output to file
echo "Hello World" > output.txt
Example:
# Error to screen (default)
ls /nonexistent
# Error to file
ls /nonexistent 2> error.txt
Default behavior:
┌─────────┐ stdin ┌─────────┐ stdout ┌─────────┐
│Keyboard │ ───────────▶ │Command │ ───────────▶ │Terminal │
└─────────┘ │ │ │Screen │
│ │ stderr │ │
└─────────┘ ───────────▶ └─────────┘
With redirection:
┌─────────┐ stdin ┌─────────┐ stdout ┌─────────┐
│File │ ───────────▶ │Command │ ───────────▶ │Output │
│input.txt│ │ │ │File │
└─────────┘ │ │ stderr └─────────┘
│ │ ───────────▶ ┌─────────┐
└─────────┘ │Error │
│File │
└─────────┘
# View file descriptors for current shell
ls -la /proc/$$/fd
# Example output:
# 0 -> /dev/pts/0 (stdin - terminal)
# 1 -> /dev/pts/0 (stdout - terminal)
# 2 -> /dev/pts/0 (stderr - terminal)
Next: → Output Redirection
Previous: ← Learning Objectives
Lesson Home: ↑ Lesson 4: Redirects & Pipes
Course Home: ⌂ Introduction to Linux