stdin, stdout, and stderr: The Three Streams Every Command Lives On
Every program Linux runs is wired to three data streams — standard input, standard output, and standard error — and learning to tell them apart is the single highest-leverage step toward fluency with the shell.
What Are stdin, stdout, and stderr?
When you run a command, Linux doesn’t just run it in a vacuum. It connects the command to three pre-opened data channels:
- stdin (standard input, file descriptor 0) — the stream data flows into the command. By default that’s your keyboard.
- stdout (standard output, file descriptor 1) — the stream the command’s normal results flow out on. By default that’s your terminal.
- stderr (standard error, file descriptor 2) — a separate channel for error messages, warnings, and diagnostics, so they never get mixed in with real results.
ls /etc # stdout → terminal, fd 1
ls /etc 2> errs # stdout still → terminal, stderr → file 'errs'
Why Learn This?
- It explains behavior that otherwise looks random. Why does
ls > out.txtgive you a clean file but errors still scream at you? Now you know why. - It’s the foundation of pipes and redirection. You can’t redirect what you can’t name.
- Scripts depend on it. Checking exit codes, capturing errors without polluting results, and debugging failures all revolve around these streams.
- It’s asked in interviews for sysadmin and backend roles — and for good reason.
Think of the three streams as one program with three wires: input comes in one, results leave through another, and problems take a third so they never contaminate the data.
1. Meet the Three Streams
Run a command that produces both results and errors, and notice how they arrive separately:
ls /etc/passwd /no-such-file
You see something like:
/etc/passwd
ls: /no-such-file: No such file or directory
/etc/passwd went to stdout; the complaint went to stderr. The terminal displays both interleaved, but underneath they’re two different streams with two different file descriptors:
| Stream | Name | Descriptor | Default destination | Carries |
|---|---|---|---|---|
| stdin | Standard input | 0 | Keyboard | Data fed into the command |
| stdout | Standard output | 1 | Terminal | The command’s real results |
| stderr | Standard error | 2 | Terminal | Errors, warnings, diagnostics |
You can see them listed when you check a process’s open descriptors:
ls -l /proc/self/fd/
# lrwx------ 1 user user 64 Dec 4 12:00 0 -> /dev/pts/0 (stdin)
# lrwx------ 1 user user 64 Dec 4 12:00 1 -> /dev/pts/0 (stdout)
# lrwx------ 1 user user 64 Dec 4 12:00 2 -> /dev/pts/0 (stderr)
2. Sending stderr to a File
Because stderr is a separate stream, you can catch it with its own descriptor number:
ls /etc/passwd /no-such-file 2> errors.txt
Now the results stay on screen, but the complaint lands in errors.txt. To send a command’s real output somewhere instead:
ls /etc > listing.txt
> sends stdout (descriptor 1) to a file. 2> sends stderr (descriptor 2) to a file. They’re independent, and you can use both at once:
ls /etc /no-such-file > listing.txt 2> errors.txt
listing.txt gets the good data; errors.txt gets the complaints; the terminal gets quiet.
3. Merging the Streams
Sometimes you want everything in one place — for example, logging a command’s full output including its errors. That’s merging, and the syntax looks deceptively simple:
ls /etc /no-such-file > all.log 2>&1
2>&1 means “send stderr where stdout is currently pointing.” Read it literally: descriptor 2 goes to the same place as descriptor 1. The whole file all.log now contains both results and errors.
Order matters. This variant does not work:
ls /etc /no-such-file 2>&1 > all.log # WRONG for the usual goal
Why? Redirections are applied left to right. 2>&1 first sends stderr to where stdout points right now (the terminal), and only then does > all.log move stdout. So errors still hit the screen while results go to the file. To capture both in the file, put > file first and 2>&1 second.
You can also merge both into the terminal while keeping normal behavior — this is a no-op you’ll see occasionally:
some-command > /dev/tty 2>&1 # forces output even when stdout is piped
4. Why Errors Go to a Separate Stream
The split isn’t an accident — it makes commands composable. Consider:
curl -s https://example.com | wc -l
curl writes the page body to stdout, which flows into wc -l. But if the server is unreachable, curl writes the failure message to stderr, which bypasses the pipe entirely and shows up on your screen instead of being counted as content. That’s exactly what you want: results flow, complaints shout.
If everything shared one stream, the error text would be piped into wc -l and silently counted as lines — corrupting your results without telling you.
5. Reading stdin
So far stdout and stderr carry data out. stdin carries data in, and many commands read from it when no file is given. The classic demo is cat:
cat
# Now type something — each line is echoed back
# Press Ctrl+D to send end-of-input and exit
cat with no arguments reads stdin until you press Ctrl+D (end-of-file). This is how commands like grep, wc, and sort can accept input from a pipe or the keyboard instead of a file.
You can also feed stdin from a file without changing the command:
wc -l < notes.txt
< notes.txt replaces stdin (the keyboard) with the file’s contents. The command itself is unchanged — it doesn’t know the difference.
6. Checking What a Command Consumes and Produces
A useful debugging habit: separate the streams and look at each.
some-command > out.txt 2> err.txt
Run it, then inspect:
out.txt → everything that is real output
err.txt → everything the command complained about
If err.txt is empty, the command produced no errors. If out.txt is empty, the command produced no usable output — which usually points to the actual problem. This single trick resolves a surprising fraction of “it didn’t work” mysteries.
Common Pitfalls
- Assuming one output stream. If you redirect with
>and errors still appear on screen, that’s not a bug — errors travel on stderr. Redirect them too with2>or2>&1. - Putting
2>&1before> file. Redirections happen left to right.2>&1 > filesends stderr to the terminal and stdout to the file — usually the opposite of what you want. Write> file 2>&1instead. - Forgetting that
>overwrites. Both>and2>truncate the target file first. Use>>and2>>to append. - Merging when you meant to keep separate.
2>&1is fine for logs, but if you’re parsing output in a script, keeping stderr separate lets you check whether the command actually succeeded. - Confusing “no output” with “failed silently”. Some commands produce nothing when they succeed. Check the exit status with
echo $?before concluding something broke.
Related Pages
| Page | Why You’d Read It Next |
|---|---|
| Redirection | The full operator reference: >, >>, <, 2>, &>, tee. |
| Pipes | What happens when stdout flows into another command’s stdin. |
| head and tail | Commands that make stream slicing concrete. |
| Keyboard Shortcuts | Ctrl+D (end of input) and Ctrl+C (interrupt) in action. |
stdin / stdout / stderr Cheatsheet
| Task | Command |
|---|---|
| List a process’s open streams | ls -l /proc/self/fd/ |
| Send stdout to a file | command > file.txt |
| Send stderr to a file | command 2> err.txt |
| Append stdout / stderr | command >> out.txt / command 2>> err.txt |
| Merge stderr into stdout | command > all.log 2>&1 |
| Read a file as stdin | command < file.txt |
| Echo input back (test stdin) | cat then Ctrl+D |
| See where a stream is going | ls -l /proc/<pid>/fd/ |