Pipes — joining commands
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
The big idea: pipes
- This is what makes the shell powerful. A pipe
|sends one command's output straight into the next command:
cat fruits.txt | sort
catproduces the lines;sortreceives them and puts them in order. No file in between.
Build a chain
- You can join as many as you like, like an assembly line:
cat fruits.txt | sort | uniq
sortorders the lines, thenuniqremoves neighbouring duplicates — so you get each fruit once.
Small tools, big results
- Each command does one job well. Pipes let you combine them to answer real questions:
cat fruits.txt | sort | uniq | wc -l
- "How many different fruits are there?" — sort, dedupe, then count. That is the Unix way.
Send the output of cat fruits.txt into wc -l with a pipe |, to count the lines.
Click Run to see the output here.
List each fruit once, in order. Pipe sort into uniq: cat fruits.txt | sort | uniq.
Click Run to see the output here.