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.
ls -l /proc/$$/fdThe 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.
ls /no/such/pathThe 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.
ls /no/such/path /workspace > listing.txtThe 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.
echo "result"
echo "problem" >&2Both 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.
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.
printf 'one\ntwo\n' | catThe 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.
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.
ls /workspace /no/such > out.txt
ls /workspace /no/such 2> err.txt
cat out.txtThe 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.