Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Text Processing  ›  Lesson

stdin, stdout, stderr — Streams, Pipes and tee

stdout / stdin / stderr 12 minEvery process reads stdin (0) and writes stdout (1) and stderr (2), and a pipe connects one command's stdout to the next command's stdin.

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 rows

Only 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
3

Now 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.

How many rows did Asha pick?Step 1 of 4
  1. Stage 1

    grep asha harvest.csv puts 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.

quick drill

Which stream number is standard error?

quick drill

A pipe connects which two streams?

quick drill

What does tee do in the middle of a pipeline?

quick drill

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, and tee saves a copy of a stage without stopping the flow.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainNarrate a pipeline

Describe what is on the conveyor belt at each stage when grep asha harvest.csv is piped into tee asha.csv and then into wc -l. Explain what would happen to an error message from grep.