stdin, stdout, stderr — Streams, Pipes and tee
Linux tools are deliberately small. sort only sorts, and wc only counts. The power comes from connecting them: the output of one flows straight into the next, with no temporary files in between. To connect them well, you need to know the three channels every program has.
The rule
In on 0, out on 1, errors on 2; a pipe joins 1 to 0.
Every process starts with three open streams, each with a number called a file descriptor:
| Number | Name | Default |
|---|---|---|
| 0 | stdin, standard input | the keyboard |
| 1 | stdout, standard output | the screen |
| 2 | stderr, standard error | the screen |
stdout and stderr both show up on the screen, but they are separate channels. That's why the redirects you learned earlier can split them apart (> for 1, 2> for 2).
The pipe, written as a vertical bar between two commands, connects the stdout of the left command to the stdin of the right one. For this whole section, Asha's data lives in ~/garden/harvest.csv:
date,crop,kg,picker
2026-06-01,tomato,3.5,asha
2026-06-01,basil,0.4,ravi
2026-06-02,tomato,2.0,ravi
2026-06-03,chilli,0.8,asha
2026-06-03,tomato,4.1,meera
2026-06-04,basil,0.6,asha$ cat harvest.csv | wc -l # 7 lines
$ grep tomato harvest.csv | wc -l # 3 tomato rowsOnly stdout goes through a pipe. Error messages still go to the screen unless you add 2>&1 before the pipe.
tee is a T-junction for a pipeline: it copies its stdin to a file and passes it on to stdout, so you can save a middle stage and keep going:
$ grep tomato harvest.csv | tee tomatoes.csv | wc -l
3Now tomatoes.csv holds the three tomato rows, and the count still appears. tee -a appends instead of replacing.
The shortcut
Read a pipeline left to right as a conveyor belt. At each bar, ask: what text is on the belt right now? Say it in plain words before you add the next command.
Stage 1
grep asha harvest.csvputs her three rows on the belt (tomato, chilli, basil).
The classic mistake is expecting errors to travel down the pipe. If grep can't open a file, the "No such file" message goes straight to the screen, and the next command never sees it.
Drills
Answer fast — trust the rule.
Which stream number is standard error?
A pipe connects which two streams?
What does tee do in the middle of a pipeline?
grep basil harvest.csv is piped into wc -l. What is printed?
Remember
- In on 0, out on 1, errors on 2; a pipe joins 1 to 0.
- Read a pipeline like a conveyor belt and say what's on it at each stage.
- Errors skip the pipe unless you add
2>&1, andteesaves a copy of a stage without stopping the flow.
