Hone

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.log
Seven lines. This is what the next stage would receive.
cat access.log | grep 500
grep read those seven lines and kept two.
grep 500 access.log
The 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

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.

Practise what a pipe carries on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.