~/shio.sh/learn/piping $ ./read
chapter 07 · learn the terminal
Piping and chaining
A pipe sends one command's output into another. Redirects write to files. && only runs the next if the previous succeeded. The aha chapter.
$ the pipe — |
This is the chapter where the terminal stops feeling like a list of commands and starts feeling like a language.
The pipe character | takes the output of one command and uses it as the input of the next. You read it left to right, like a sentence.
cat notes.txt | grep saltThat says: show me notes.txt, and from that, only the lines containing "salt." The same as grep salt notes.txt, yes — but with a pipe, you can chain as many steps as you want.
› try: cat notes.txt | grep salt
$ redirects — > and >>
You met > already in chapter 4. It writes a command's output into a file (overwriting). >> does the same but appends to the end of the file instead.
ls > listing.txt— overwritelisting.txtwith the listingecho "another line" >> notes.txt— add a line to the end ofnotes.txt
› try: ls > listing.txt
$ && and || — chaining by success
Sometimes you want command B to run only if command A succeeded. That's &&.
mkdir new-thing && cd new-thingIf the mkdir succeeds, the cd runs. If it fails, the second half is skipped — saving you from cd'ing somewhere that doesn't exist.
|| is the opposite: B runs only if A failed.
cat secret.txt || echo "no secret here"$ the aha moment
Every Unix tool does one small thing. The pipe lets you compose them into something larger. That's the whole philosophy. Short verbs, joined together.
When you find yourself reaching for a complicated GUI feature, ask: could this be three small commands and two pipes? It usually can.
~/shio.sh/learn/piping $ cd ..