$_ CLI In Depth
Learning progress 0%
lesson streams redirection pipes

stdin, stdout & stderr

5 min read Module 7: Streams, Redirection & Pipes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:12

Three numbered slots

A process inherits a table of open files from its parent. Each entry has a number called a file descriptor. By convention three of them matter for almost every program. File descriptor 0 is standard input, which is where the program reads its data. File descriptor 1 is standard output, where normal results go. File descriptor 2 is standard error, where diagnostics and error messages go. The program does not know or care what these slots are connected to. A terminal, a file, a pipe, and a network socket all look the same to a program that reads from slot 0 or writes to slot 1.

The shell is responsible for setting the slots before it starts a command. In an interactive session all three point at the terminal, which you can confirm with the kernel view of the shell process.

BASH1 lines
ls -l /proc/$$/fd

The listing shows entries 0, 1, and 2 as links to the pseudo-terminal, along with a higher-numbered entry that bash keeps for itself. The exact numbers differ from one session to another, but the three standard slots always point at the same device when nothing has been redirected.

Why the split matters

Separating normal output from errors is what makes the shell useful for scripting. If a command prints its results to slot 1 and its complaints to slot 2, you can send the results into a file while the complaints still appear on your screen. The same separation lets a pipeline pass results along while error messages bypass it. Programs that mix both kinds of text on one stream make this impossible, which is why the convention is so widely followed.

A quick way to see the split is to ask for a file that does not exist. The error goes to slot 2, so it appears on the terminal, and nothing reaches slot 1.

BASH1 lines
ls /no/such/path

The message ls: cannot access appears, and the exit status is 2. Now run the same command with its standard output redirected to a file. The error still appears on the screen, because redirecting slot 1 does not affect slot 2.

BASH1 lines
ls /no/such/path /workspace > listing.txt

The directory listing of /workspace is written into listing.txt, and the error about the missing path is still printed to the terminal. This is the single most useful fact in this module: a redirection applies to one slot, and the others keep their original destinations.

Writing to the slots yourself

A program can write to any slot it likes, and a shell script can too. The printf builtin writes to standard output by default. Writing to standard error means naming slot 2 explicitly with >&2. Scripts use this to report problems in a way that does not pollute the data they produce.

BASH2 lines
echo "result"
echo "problem" >&2

Both lines appear on the terminal. If you redirect the script to a file, only the first line goes to the file. Anyone who calls the script can separate the two streams without reading the script itself.

Tip

Send every diagnostic message in a script to standard error. Standard output should carry only the data that another program might consume.

Standard input and the terminal

Standard input is the slot that the read builtin, cat, and most filters read from when they are given no file name. In an interactive shell it is connected to the keyboard through the terminal line discipline you met earlier. That is why cat with no arguments waits for you to type. When the input comes from a file or a pipe instead, the same program reads the data without any change to its code.

BASH1 lines
printf 'one\ntwo\n' | cat

The output is one and two. The cat program read slot 0, which the pipe had connected to the output of printf, and wrote the lines to slot 1, which is the terminal. A later lesson turns this pattern into the everyday pipeline.

Checking the exit status of a program

The slots carry data, but the exit status carries the result of the whole run. It does not travel through a slot, and you read it from $? after the command finishes. A program that fails may still have written some output, so a careful script checks both the status and what was written. The two channels answer different questions: the status says whether the program succeeded, and the streams say what it produced and what it complained about.

BASH2 lines
ls /workspace > ok.txt; echo "status=$?"
ls /no/such/path > bad.txt; echo "status=$?"

The first status is 0 and the second is 2. In the second case bad.txt is empty, because nothing was written to slot 1, and the error message appeared on the terminal. Keeping both facts in mind makes redirection behavior predictable.

Hands-on: see the three slots

Run the following commands and compare what appears on the screen with what lands in the file. The first sends standard output to a file, the second sends standard error to a file, and the third reads from standard input.

BASH3 lines
ls /workspace /no/such > out.txt
ls /workspace /no/such 2> err.txt
cat out.txt

The first ls writes the listing into out.txt and the error to the terminal. The second writes the error into err.txt and the listing to the terminal. Reading out.txt at the end shows only the listing. This is the working model for every redirection you will see in the rest of the module.

Redirections change the file descriptor table

Visualizer

Redirections are applied left to right, before the command runs. >file 2>&1 and 2>&1 >file look alike, but they send stderr to different places.

1 / 4
Before the command
0/dev/pts/0
1/dev/pts/0
2/dev/pts/0
$ ls /nope >out.txt 2>&1

Bash starts with fds 0, 1 and 2 all pointing at the terminal. Redirections change this table before ls runs, and ls inherits whatever the table says.

ls /nope >out.txt 2>&1; echo "status $?"; cat out.txt
Terminal lab · 3 steps · real Linux in your browser

Split the streams

Look at the descriptors and send standard output and standard error to different files.

01
Read your descriptors
Save the list of open descriptors of your shell to a file: ``bash ls -l /proc/$$/fd > fds.txt ``
Show hint for step 1
$$ is the shell process ID, and /proc/PID/fd holds one entry per open descriptor.
02
Send errors and results to different files
Run ls on the workspace and on a path that does not exist. Send the listing to listing.txt and the error to errors.txt: ``bash ls /workspace /no/such > listing.txt 2> errors.txt ``
Show hint for step 2
> changes descriptor 1 and 2> changes descriptor 2, so each stream can have its own destination.
03
Write diagnostics to standard error
Create diag.sh, which prints ok to standard output and warn to standard error, then run it so that each stream goes to its own file: ``bash printf 'echo ok\necho warn >&2\n' > diag.sh bash diag.sh > out.txt 2> err.txt ``
Show hint for step 3
Use >&2 inside the script to write to standard error.
Check yourself Question 1 of 4
Concept Check Descriptor for standard error recall

Which file descriptor number is standard error?

Recap & Key Takeaways

Every process has standard input on descriptor 0, standard output on 1, and standard error on 2, and programs do not care what those slots are connected to.
A redirection changes one slot and leaves the others alone, so errors can still reach the terminal while results go into a file.
Write diagnostics to standard error with >&2 so that standard output carries only data.
The exit status is separate from the streams and is read from $? after the command finishes.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue