IntroToLinux

1. Understanding Standard Streams

Every Linux process has three default streams:

Visual Overview

┌─────────────────────────────────────────────────────────────┐
│                    Linux Process                            │
│                                                             │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐      │
│  │   stdin     │    │   stdout    │    │   stderr    │      │
│  │     (0)     │    │     (1)     │    │     (2)     │      │
│  └─────────────┘    └─────────────┘    └─────────────┘      │
│         ▲                   │                   │           │
└─────────┼───────────────────┼───────────────────┼───────────┘
          │                   │                   │
          │                   ▼                   ▼
    ┌─────────┐         ┌─────────────┐     ┌─────────────┐
    │Keyboard │         │   Terminal  │     │   Terminal  │
    │  Input  │         │   Screen    │     │   Screen    │
    └─────────┘         │  (normal)   │     │  (errors)   │
                        └─────────────┘     └─────────────┘

Standard Input (stdin) - File Descriptor 0

Example:

# Reading from keyboard (default)
cat                    # Type something, press Ctrl+D to end

# Reading from file
cat < input.txt        # Redirect file to stdin

Standard Output (stdout) - File Descriptor 1

Example:

# Output to screen (default)
echo "Hello World"

# Output to file
echo "Hello World" > output.txt

Standard Error (stderr) - File Descriptor 2

Example:

# Error to screen (default)
ls /nonexistent

# Error to file
ls /nonexistent 2> error.txt

Stream Redirection Examples

Default behavior:
┌─────────┐    stdin     ┌─────────┐    stdout    ┌─────────┐
│Keyboard │ ───────────▶ │Command  │ ───────────▶ │Terminal │
└─────────┘              │         │              │Screen   │
                         │         │    stderr    │         │
                         └─────────┘ ───────────▶ └─────────┘

With redirection:
┌─────────┐    stdin     ┌─────────┐    stdout    ┌─────────┐
│File     │ ───────────▶ │Command  │ ───────────▶ │Output   │
│input.txt│              │         │              │File     │
└─────────┘              │         │    stderr    └─────────┘
                         │         │ ───────────▶ ┌─────────┐
                         └─────────┘              │Error    │
                                                  │File     │
                                                  └─────────┘

ℹ️ Practical Example

# 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