Shell Pipes & Pipeline Processing
Chain Linux commands together using the pipe operator (|) to construct powerful data processing pipelines.
What is a Pipe (|)?
The pipe operator | connects the stdout of the command on its left directly to the stdin of the command on its right. Data streams in memory between processes without creating temporary files on disk.
[ Command A ] ──(stdout)──> [ Pipe | ] ──(stdin)──> [ Command B ]Classic Pipeline Examples
1. Count Total Lines Matching a Pattern
# Count number of 404 errors in web log
cat /var/log/nginx/access.log | grep " 404 " | wc -l2. Extract Top 10 Most Frequent IP Addresses
cat /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -n 103. Find Large Files and Format Display
du -sh * | sort -hr | head -n 5Inspecting Intermediate Stream Data (tee)
tee splits a stream, writing data both to a file and continuing down the pipe to stdout:
# Save intermediate filtered results while continuing pipeline
cat data.csv | grep "active" | tee active_users.csv | wc -lCritical Bash Note: pipefail
By default, Bash evaluates a pipeline's exit code based only on the final command in the chain. In scripts, always enable set -o pipefail so failures anywhere in the pipeline trigger an error:
set -o pipefailStandard Streams: stdin, stdout, and stderr
Master Unix standard I/O streams (File Descriptors 0, 1, 2), stream pipelines, and discarding output with /dev/null.
Shell I/O Redirection (>, >>, <, 2>&1)
Redirect standard input, overwrite files with >, append with >>, capture stderr with 2>, and merge streams with 2>&1.