Pipes
Core Idea
A pipe connects stdout from one process to stdin of another process.
The pipe character | is shell syntax, not a command. Bash creates the pipe, starts both commands, and connects their file descriptors.
First Pipeline
history | grep ssh
Read it left to right:
historyprints text to stdout.|connects that stdout to the next command’s stdin.grep sshreads stdin and prints matching lines.
Build Pipelines Slowly
history
history | grep ssh
history | grep ssh | wc -l
Add one stage only after the previous stage makes sense.
What Crosses The Pipe
Bytes cross the pipe. Usually those bytes are text lines, but the shell does not care. Tools such as grep, sort, uniq, cut, and wc cooperate because they can read stdin and write stdout.
Common Patterns
Count matches:
grep -R makers ~/src/pages | wc -l
Sort and deduplicate:
grep -R '^#' ~/src/pages | sort | uniq
Keep a copy while continuing the pipeline:
history | tee ~/playground/history.txt | grep ssh
Watch Out
- A pipeline can hide which stage failed. Test stages separately.
- Some commands wait for stdin. Press
Ctrl-Cif a pipeline hangs. - Pipeline exit status has details;
set -o pipefailmakes script failures louder. - Do not use
cat file | commandwhencommand fileworks, unless you are teaching stdin deliberately.
Proof Check
Run a three-stage pipeline and explain exactly what each stage reads and writes.
Docs Pointers
- Run
man bashand search forPipelines. - Read I/O, file descriptors, stream redirection, one-liners, regular expressions, sort, and uniq.

Linux Foundations