Pipes
By the end of this lesson you'll be able to chain small commands into one powerful pipeline, send output and errors exactly where you want, and combine commands so they run in the right order — the skills that turn the terminal from a toy into a tool.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Cli course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
1. The Three Streams
Before pipes make sense, you need to know that every command has three channels . stdin (channel 0 ) is where input arrives. stdout (channel 1 ) is where normal results go. stderr (channel 2 ) is a separate channel just for error messages. On screen, stdout and stderr look the same, but keeping them apart is what lets you save real results while still seeing warnings — and it's the reason a later trick ( 2 &1 ) exists at all.
2. The Pipe Operator |
The pipe ( | ) connects the stdout of the command on its left to the stdin of the command on its right. That lets you build a chain of tiny, single-purpose tools — list | filter | count — where data flows left to right. This is the Unix philosophy in action: "do one thing well," then combine. Read this worked example, run it, and watch each stage shrink the data.
Don't write a five-command pipe in one go. Run grep ERROR server.log , check it looks right, then add | sort , check again, then | uniq -c , and so on. Each | you add only sees the output of the stage before it, so building up gradually makes mistakes obvious immediately.
Your turn. The pipeline below is almost complete — fill in the blank marked ___ using the hint in the comment, then run it.
3. Redirection: , , , 2 , 2 &1
Redirection points a stream at a file instead of the screen or keyboard. writes output to a file and overwrites it; appends to the end. feeds a file in as stdin. To capture errors you must name the channel: 2 redirects stderr on its own, and 2 &1 means "send stderr to wherever stdout is going" — so file 2 &1 puts both into one file. The order matters : redirect stdout first, then merge stderr into it.
4. /dev/null, tee, and Command Chaining
/dev/null is a special file that throws away anything written to it — use it to silence output you don't care about. tee does the opposite of hiding: it splits a stream so it goes to a file and the screen at once. Finally, command chaining sequences whole commands: ; runs the next one regardless, && runs it only if the previous command succeeded , and || runs it only if the previous command failed .
Now you try. Fill in the two blanks with the right chaining operators using the hints, then run it.
No blanks this time — just a brief and an outline to keep you on track. Combine a pipe, tee , and && into a single line, run it, and check your output against the comments. This is exactly the kind of one-liner you'll reach for on real servers.
Practice quiz
What does the pipe | operator do?
- Deletes output
- Sends one command's stdout into the next command's stdin
- Renames a file
- Runs a command as root
Answer: Sends one command's stdout into the next command's stdin. A pipe connects one command's stdout to the next command's stdin.
Which channel number is stderr?
- 0
- 1
- 2
- 3
Answer: 2. stdin is 0, stdout is 1, and stderr is 2.
What does > do to an existing file?
- Appends to the end
- Overwrites it
- Reads it as input
- Deletes only errors
Answer: Overwrites it. > overwrites the file, erasing the old contents.
Which operator appends output to the end of a file?
- >>
- <
- |
- 2>
Answer: >>. >> appends and keeps the existing contents.
What does 2> file.txt capture?
- Both streams
- Only stdout
- Only stderr
- Nothing
Answer: Only stderr. 2> redirects only stderr (channel 2) to the file.
What does 2>&1 mean?
- Send stderr to wherever stdout is going
- Delete both streams
- Send stdout to a file
- Run two commands
Answer: Send stderr to wherever stdout is going. 2>&1 merges stderr into wherever stdout currently points.
What is /dev/null used for?
- Saving backups
- Listing files
- Discarding output you do not care about
- Counting lines
Answer: Discarding output you do not care about. /dev/null is the trash can; anything written to it disappears.
What does the && operator do?
- Runs the next command only if the previous one succeeded
- Always runs the next command
- Runs the next command only if the previous failed
- Pipes output
Answer: Runs the next command only if the previous one succeeded. && runs the next command only if the previous one succeeded (exit 0).
What does the || operator do?
- Runs the next command regardless
- Runs the next command only if the previous succeeded
- Pipes stderr
- Runs the next command only if the previous failed
Answer: Runs the next command only if the previous failed. || runs the next command only if the previous one failed.
What does the tee command do?
- Discards a stream
- Sorts input
- Counts words
- Writes to a file AND shows it on screen
Answer: Writes to a file AND shows it on screen. tee splits a stream: it saves to a file and passes it on to the screen.