Redirection & Pipes¶
Controlling where command input comes from and where output goes is fundamental to Unix philosophy.
Standard Streams¶
Every process has three standard streams:
| Stream | Number | Default | Purpose |
|---|---|---|---|
| stdin | 0 | Keyboard | Input |
| stdout | 1 | Terminal | Normal output |
| stderr | 2 | Terminal | Error messages |
┌──────────┐ ┌─────────┐ ┌──────────┐
│ stdin │────>│ Command │────>│ stdout │
│ 0 │ │ │────>│ stderr │
└──────────┘ └─────────┘ └──────────┘
Output Redirection¶
Redirect stdout to File¶
Redirect stderr to File¶
Redirect Both¶
command > output.txt 2>&1 # Both to same file
command &> output.txt # Shorthand (bash 4+)
command >> output.txt 2>&1 # Append both
command &>> output.txt # Append shorthand (bash 4+)
Redirect to Different Files¶
Discard Output¶
command > /dev/null # Discard stdout
command 2> /dev/null # Discard stderr
command &> /dev/null # Discard both
Input Redirection¶
From File¶
Here Documents (heredoc)¶
Inline multi-line input:
With variable expansion:
Without expansion (quote the delimiter):
Indented heredoc (tabs stripped):
Here Strings¶
Single-line input:
Pipes¶
Connect stdout of one command to stdin of another:
Basic Examples¶
ls -l | head -5 # First 5 lines
cat file.txt | wc -l # Count lines
history | grep "git" # Search history
ps aux | grep nginx # Find process
Practical Pipelines¶
# Find 10 largest files
du -sh * | sort -rh | head -10
# Count unique words
cat file.txt | tr ' ' '\n' | sort | uniq -c | sort -rn
# Extract and format data
cat data.csv | cut -d',' -f2 | sort | uniq
# Monitor log for errors
tail -f app.log | grep -i error
Pipe to Multiple Commands (tee)¶
tee writes to both file and stdout:
command | tee output.txt # Show and save
command | tee -a output.txt # Show and append
command | tee file1 file2 # Multiple files
Example - save and continue processing:
Pipe stderr¶
By default, pipes only pass stdout. To include stderr:
Process Substitution¶
Treat command output as a file:
Examples¶
Compare two command outputs:
Compare sorted files:
Use where file is expected:
File Descriptors¶
Creating Custom Descriptors¶
Read and Write¶
Common Patterns¶
Save stdout and stderr Separately¶
Process Both Streams Separately¶
Show Output and Save¶
Check if stdin is Terminal¶
Pipeline with Error Checking¶
By default, pipeline exit status is the last command's. Use pipefail:
Get individual exit statuses:
Noclobber¶
Prevent accidental overwrite:
set -o noclobber
echo "test" > existing.txt # Error if exists
echo "test" >| existing.txt # Force overwrite
set +o noclobber # Disable
xargs - Build Commands from Input¶
Convert input to arguments:
Handle spaces in filenames:
Limit arguments per command:
Run with placeholder:
Parallel execution:
Redirection Order Matters¶
The order of redirections is processed left to right:
# These are different:
command > file 2>&1 # stderr goes to same file as stdout
command 2>&1 > file # stderr goes to terminal, stdout to file
Summary Table¶
| Syntax | Meaning |
|---|---|
> | Redirect stdout (overwrite) |
>> | Redirect stdout (append) |
2> | Redirect stderr |
2>> | Redirect stderr (append) |
&> | Redirect both (bash 4+) |
2>&1 | Redirect stderr to stdout |
< | Input from file |
<< | Here document |
<<< | Here string |
\| | Pipe stdout |
\|& | Pipe both (bash 4+) |
<() | Process substitution (input) |
>() | Process substitution (output) |
Try It¶
-
Practice basic redirection:
-
Redirect stderr:
-
Use pipes:
-
Use tee:
-
Process substitution:
-
Clean up: