C.W.K.
Stream
Lesson 01 of 10 · published

Standard Streams

~12 min · stdin, stdout, stderr, fd

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Three doors every program has

Every Unix process is born with three open file descriptors: stdin (0), stdout (1), stderr (2). They are how the kernel and the shell talk to your program. The shell's pipes and redirections are nothing more than wiring up these three doors to different things.

Default wiring

  • stdin (0) — your keyboard.
  • stdout (1) — your terminal screen.
  • stderr (2) — also your terminal screen, but a separate stream so errors don't mix into a pipe.

Why two output streams?

Imagine find / -name foo 2>/dev/null | grep bar. The errors ("Permission denied") go to stderr, which you silence with 2>/dev/null. The actual matches go through the pipe to grep. If find used a single stream, you couldn't separate the noise from the data. This split is the most useful design decision in Unix.

The numeric handles

You'll see numbers in redirections: 2> (stderr), 1> (stdout, default), 2>&1 (merge stderr into stdout). These are file descriptor numbers — direct kernel-level handles. You can open new ones too: exec 3> file opens fd 3, then echo hello >&3 writes to it.

Code

See the three streams in action·bash
# stdout vs stderr — both show up by default
echo 'hello'                    # stdout
ls /no/such/dir                 # stderr only
# Silence stderr
ls /no/such/dir 2>/dev/null
# Silence stdout, keep stderr
ls /etc 1>/dev/null
# Merge stderr into stdout (then pipe both)
ls /etc /no/such/dir 2>&1 | head

External links

Exercise

Run ls /etc /no/such/dir. Notice how stdout and stderr both display. Now ls /etc /no/such/dir > out.txt 2> err.txt. Read both files. Confirm you've successfully split the streams.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.