Lessons · Terminal · what a pipe carries
What a pipe actually carries
A pipe joins one command's standard output straight to the next one's standard input. Nothing is written to disk and nothing is named.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Every pipeline you will ever write is this one idea repeated. Once it is solid, a five-stage pipeline is not five times harder than a one-stage one.
How to think about it
Run the first command alone and look at what it printed. That text, exactly, is what the next command reads.
Worked example
cat access.logSeven lines. This is what the next stage would receive.
cat access.log | grep 500grep read those seven lines and kept two.
grep 500 access.logThe same answer. grep opens a file itself, so the cat was one extra process doing nothing.
Your turn
Send the contents of app.log to grep without naming the file twice.
cat app.log grep log
Try it at a real prompt
The trap
A pipe carries text, not files. The next command cannot know where the text came from, which is why grep -n in the middle of a pipeline numbers the stream and not the original file.