stdin, stdout and stderr

Beginner · 9 min

Every program starts with three streams: input, output and error, numbered 0, 1 and 2. See why errors skip pipes and how programs detect terminals.

Before thisAnatomy of a commandCreating, copying, moving and removing

Learn Linux TV · 17 min · from 1:17

stdin, stdout and stderr

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Learn Linux TVReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Every process starts with three open streams: stdin (file descriptor 0), stdout (1) and stderr (2). By default all three are connected to your terminal.
  • Programs write results to stdout and diagnostics to stderr, so a pipe or file can carry results while errors still reach your screen.
  • Many commands read stdin when given no file name: cat, wc and sort work on a pipe or on a file, and - often means stdin explicitly.
  • A program can ask whether stdout is a terminal and behave differently, which is why output formatting can change when you pipe it.
  • The exit status is separate from both streams. A command can print to stderr and still succeed, or fail without printing.

Three channels, not one

A program needs to get data in and send results out. The simplest design has each program open files by name, but then wc would need to know where its input is and ls where its output goes. Unix does something neater: every process starts with three channels already open, and the program just reads from one and writes to the others. Whoever starts the program decides what those channels are connected to.

  • Standard input (stdin), channel 0: where the program reads.
  • Standard output (stdout), channel 1: where results go.
  • Standard error (stderr), channel 2: where complaints go.

The numbers are file descriptors, small integers the kernel uses to track open files. When you type at a terminal, all three are wired to it: your keyboard feeds stdin, and both stdout and stderr print on screen. That’s why you can’t normally tell stdout from stderr by looking. Think of a kitchen with one door to the dining room for plates and a different door to the manager for problems. At the start of the day both doors open onto the same corridor.

Telling them apart

Ask ls for one file that exists and one that doesn’t:

$ ls real.txt missing.txt
ls: missing.txt: No such file or directory
real.txt
$ echo $?
1

Two messages, two streams. Now I’ll redirect one stream at a time (the next lesson covers the syntax):

$ ls real.txt missing.txt 2>/dev/null
real.txt
$ ls real.txt missing.txt >/dev/null
ls: missing.txt: No such file or directory

Discard channel 2 and only the result is left. Discard channel 1 and only the error is left. I confirmed the exit status stays 1 when stderr is thrown away: the status travels separately from both streams.

You can see the split in your own scripts. I saved a two-line script and ran it three ways:

$ cat streams.sh
echo "to stdout"
echo "to stderr" >&2
$ bash streams.sh 2>/dev/null
to stdout
$ bash streams.sh >/dev/null
to stderr

>&2 sends a command’s output to channel 2. That is how you write an error message in a script.

Pipes carry only stdout

$ ls real.txt missing.txt | wc -l
ls: missing.txt: No such file or directory
       1

wc counted 1 line, the file that exists. The error message skipped the pipe and went straight to the terminal, which is what you want: a failure in the middle of a pipeline shouldn’t get counted, sorted or parsed as data. If you do want both in the pipe, you have to merge them first, and the next lesson shows how.

Reading input

A program reads stdin whenever you don’t hand it a file. wc -l with a file name reads the file. Without one, it reads whatever is attached to channel 0:

$ printf 'one\ntwo\nthree\n' | wc -l
       3
$ wc -l < data/people.csv
       6
$ printf 'a\nb\n' | cat -n
     1	a
     2	b

Piped data, a redirected file, and the keyboard all look the same to the program. That’s the design paying off: wc has no idea whether it is counting a file or the output of another command. Many tools also accept - as a name meaning “stdin here”, so echo piped | cat - and echo piped | cat /dev/stdin both printed piped. With no pipe, cat alone sits waiting on your keyboard. It isn’t hung. Ctrl-D ends input.

Builtins use stdin too. read takes one line:

$ printf 'ada\ngrace\n' | { read first; echo "first line: $first"; read second; echo "second: $second"; }
first line: ada
second: grace

A program can tell a terminal from a pipe

Because channel 1 might be a screen or a pipe, programs can ask. The shell’s test is [ -t 1 ]. I ran the same test in a pipeline, and again under a pseudo-terminal using Python’s pty module:

$ bash -c '[ -t 1 ] && echo "stdout is a terminal" || echo "stdout is not a terminal"' | cat
stdout is not a terminal
$ (same command under a pty)
stdout is a terminal

This explains behaviour that looks like magic. ls prints columns for people and one name per line when piped. grep --color=auto colours only for terminals. I did not capture ls or grep output under the pty, because my pty harness hung on it, so those two are standard behaviour I did not see here. The test run is the proof that the check exists.

The order of output

Because stdout is usually buffered and stderr isn’t, interleaving can surprise you when output goes to a file or pipe. With three short echo lines, both streams merged into one pipe arrived in order (out1, err1, out2), but a big program writing a lot can reorder. I did not reproduce a reordering.

About the video

Learn Linux TV’s “Linux Crash Course - Data Streams (stdin, stdout & stderr)” was published 22 November 2021 and runs 17 minutes 12 seconds. I clipped 1:17 to 10:24, from “The three data streams” through “Splitting standard output and standard error” and “Number designations”, leaving out the intro and the closing part. The description’s time codes include “Example of Standard Error (stderr)” at 3:06 and “Using the echo command to find the return code” at 3:47, which touch on the exit status point in this lesson. English per title and description. I couldn’t read a transcript. The final chapter (10:24) is sending stdout and stderr to different files, which belongs to the next lesson, so I cut there. I can’t say whether the video mentions that pipes carry only stdout. That section and the terminal test are mine.

What I did not run

Linux. Interactive keyboard input to cat (my shell had no terminal), ls and grep colour behaviour, and output reordering from buffering.

Next

The streams are fixed channels, but you choose where each one points. That’s redirection: redirection.

Check yourself

  1. 01You run ls real.txt missing.txt | wc -l. The error message still appears on screen and wc prints 1. Why?

    The pipe connects only stdout (stream 1). The error goes to stderr (stream 2), which stays on the terminal, and only the one line for real.txt reached wc.

  2. 02Why do programs bother separating stdout from stderr?

    So results can be saved or piped without error messages polluting them, and errors remain visible to the person running the command.

  3. 03What are the three standard streams and their numbers?

    stdin is 0, stdout is 1 and stderr is 2. They are file descriptors every process inherits from its parent.

  4. 04cat with no arguments seems to hang. What is it doing?

    It is reading stdin, which is your keyboard, and echoing lines back. Press Ctrl-D to signal end of input or Ctrl-C to quit.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark