Standard streams
On this page
You type cat, press Enter, and get no prompt back. The program has not reported an error. It is waiting for something, but the empty screen does not tell you what.
By the end, you can name the input it waits for, distinguish ordinary output from diagnostics, and explain why both kinds of output appear in the same terminal.
Before you start
Section titled “Before you start”Finish Practice: How the Shell Interprets Input (B08). Keep your B04 lab directory. Use only ~/lab/b09/streams here:
mkdir -p ~/lab/b09/streamscd ~/lab/b09/streamsprintf 'Read the note\nCheck the result\n' > note.txtThe last line prepares a disposable two-line file. B09.2 explains its > operator. If you repeat this lesson, use Reset this lesson before repeating the setup.
The module’s replay prompts show learner@rootvolt; your prompt can show a different account and computer name.
Recognize input that comes from the terminal
Section titled “Recognize input that comes from the terminal”Predict first: with no filename, will cat print note.txt, report a missing argument, or wait?
catRun it, then compare with your guess
It waits. The captured session has no output and no prompt until Ctrl+D is pressed. Press Ctrl+D at the start of the empty input line to finish this run and return to Bash.
Do not close the terminal. The command is using a normal input source, rather than failing to respond.
B03.4 used cat without a FILE argument to read what you typed. That source is standard input, shortened to stdin. In an ordinary terminal command with no redirection, stdin is connected to the terminal: your keyboard supplies its input.
The terminal normally collects a line before passing it to cat. Ctrl+D on an empty line makes that read reach end of input. cat finishes when there is nothing more to read. Ctrl+D does not type a character into a file. With characters waiting on the line, it first makes those characters available; the empty-line case is the one used here.
Ctrl+C from B03.4 is different: it interrupts the running command. Ctrl+D lets this input-reading command finish by reaching the end of its input. At Bash’s own empty prompt, Ctrl+D can exit the shell, so use it while cat is waiting.
Follow ordinary output
Section titled “Follow ordinary output”Give cat a file argument:
cat note.txtRead the noteCheck the resultThis time cat opens the named file and reads its contents, rather than waiting on stdin. It writes the copied text to standard output, shortened to stdout. By default, stdout is connected to your terminal, so you see the two lines.
An input file argument and stdin are different ways to supply data. A program chooses whether to read its stdin. Merely having an open input connection does not require it to use that connection: cat note.txt reads its named file instead.
printf also writes ordinary output:
printf '[%s]\n' note.txt[note.txt]As in B08.2, %s prints an argument. It does not open a file just because the argument looks like a filename. Both commands write stdout, but they obtain the text differently.
Separate a diagnostic from the result
Section titled “Separate a diagnostic from the result”Predict first: when the named file does not exist, does the next line belong to the file’s contents?
cat missing.txtRun it, then compare with your guess
cat: missing.txt: No such file or directoryNo. cat cannot read that file. This line is a diagnostic explaining what prevented the read, using the message structure from B04.4.
Programs conventionally write diagnostics to standard error, shortened to stderr, separately from ordinary results. cat uses stderr for this message. That separation lets you save the result without mixing a failed-file message into it. B09.2 demonstrates the separation by redirecting the streams.
The lab terminal displays stdout and stderr in the same place. Their appearance does not identify their source; neither the terminal nor a replay gives them different colors. Read the program’s documentation and follow its connections, rather than judging a stream by how its text looks.
Give each connection its number
Section titled “Give each connection its number”A file descriptor is a numbered connection that a running program can read from or write to. These three numbers are the standard ones:
| Descriptor | Name | Direction for the command | Default terminal connection |
|---|---|---|---|
0 |
stdin | Input | Text supplied through the terminal |
1 |
stdout | Output | Ordinary results displayed in the terminal |
2 |
stderr | Output | Diagnostics displayed in the terminal |
In words: terminal input flows toward the command on descriptor 0. Ordinary output leaves the command on descriptor 1; diagnostics leave on descriptor 2. Both output paths reach the terminal by default. The model shows the standard connections, omitting terminal internals and files a command opens separately.
The numbers identify connections, not filenames or screen locations. Redirection changes where a connection leads; it does not rename stdout to stderr. A pipeline, taught in B09.3, connects one command’s stdout to another command’s stdin.
Mistakes to recognize
Section titled “Mistakes to recognize”Treating silence as failure. cat without a filename can be waiting for stdin. Check whether a prompt is present before typing another shell command.
Treating every displayed line as stdout. The missing-file diagnostic came through stderr. Sharing a terminal does not make the two output streams identical.
Expecting every command to read stdin. printf prints its supplied arguments. Giving it a filename string does not make it read that file.
Try it yourself
Section titled “Try it yourself”Goal: Read only the first line of note.txt with head. Identify where the data comes from and which standard stream carries the printed result. Say whether this run needs keyboard input.
Hint
B07.1 used -n with a count. A file argument tells head which file to open.
Solution
head -n 1 note.txtRead the notehead reads the named file and writes that line to stdout, descriptor 1. It does not need keyboard input for this run. Its stdin connection still exists, but the named file supplies the data. No diagnostic is printed to stderr.
Reset this lesson
Section titled “Reset this lesson”Finish any waiting cat first. Return to the parent and inspect this lesson’s directory:
cd ~/lab/b09ls -AR streamsrm -r ~/lab/b09/streamsRerun Before you start. If the directory is already absent, skip removal.
- Stdin, descriptor 0, supplies input; a terminal normally supplies it from your keyboard.
- Stdout, descriptor 1, carries ordinary output; stderr, descriptor 2, carries diagnostics.
- Both output streams normally reach the terminal, where their appearance can be identical.
- A command can read a named file instead of stdin. Ctrl+D on an empty line ends the terminal input read used here.
Next, Redirection changes these connections to read and write files.
Further reading
Section titled “Further reading”- On your system:
man cat,man head, andman 3 stdin. - Ubuntu 24.04: stdin(3) and cat(1).
- GNU Coreutils: cat invocation.
- GNU Bash: Redirections.