Pipes & Redirection
stdin/stdout/stderr, pipes, here-docs, process substitution, and tee for multi-destination output.
Overview
Overview Every process inherits three file descriptors: 0 (stdin), 1 (stdout), 2 (stderr). Redirection changes where these point.
Redirection Operators
- > file — redirect stdout to file (truncating)
- >> file — append stdout to file
- 2> file — redirect stderr to file
- 2>&1 — merge stderr into stdout
- &> file — redirect both stdout and stderr to file
Pipes and Subshells
The pipe operator | connects stdout of the left command to stdin of the right. Commands in a pipeline run in subshells — variable assignments in a pipeline do not affect the parent shell.
tee and Named Pipes
tee copies stdin to both stdout and a file:
Named pipes (FIFOs) created with mkfifo allow producer-consumer patterns between separate processes.
Key concepts
- File descriptors 0/1/2
- stdin (0), stdout (1), stderr (2). Redirection changes which file or pipe these descriptors point to.
- 2>&1
- Redirect stderr (fd 2) to wherever stdout (fd 1) currently points. Order matters: cmd > file 2>&1 is correct; cmd 2>&1 > file sends stderr to terminal.
- Pipeline subshell
- Each command in a pipeline runs in a subshell. Variable assignments inside pipelines do not affect the parent shell.
- Process substitution
- <(cmd) substitutes command output as a named pipe. Enables comparing live output without temp files.
- tee
- Reads stdin and writes to both stdout and one or more files. tee -a appends instead of truncating.
When to use it
- Building complex data-processing pipelines in shell scripts
- Explaining why variables set inside a pipeline do not persist after the pipeline
- Logging output while still displaying it to the terminal
Interview questions
- Why does this script not work: count=0; cat file.txt | while read line; do count=$((count+1)); done; echo $count?
- You need to log all output of a script to a file while still displaying it on the terminal. How?
Sign up free to read the full topic
More Linux topics
Start free on Cariara · Coding problems · System design studio