Standard File Descriptors

Every running program under Unix starts with three files already opened

Table 1-1. Standard Files Provided by Unix

Descriptive NameFile NumberDescription
Standard In0Input from the keyboard
Standard Out1Output to the console
Standard Error2Error output to the console

So by default, all input will come from the keyboard and all output (both normal and error output) will go to the screen.

Figure 1-2. Default Unix Files

The Shell

The shell is your gateway to interacting with the operating system. It has only one major task -- to allow you to execute programs (you will begin to understand how the shell actually does this when we talk about some of the internals of the operating system later).

But modern shells do much more than allow you to simply execute a program. They have powerful abilities to redirect files, allow you to execute multiple programs simultaneously and script complete programs.

Redirection

Often we do not want the standard files mentioned above to point to their default places. For example, you may wish to capture all the output of a program into a file on disk, or, alternatively have it read its commands from a file you prepared earlier. Another useful task might like to pass the output of one program to the input of another. It should come as no surprise you shell allows you to do all of this (and more!)

Table 1-2. Standard Shell Redirection Facilities

NameCommandDescriptionExample
Redirect to a file> filenameTake all output from standard out and place it into filename. Note using >> will append to the file, rather than overwrite it.ls > filename
Read from a file< filenameCopy all data from the file to the standard input of the programecho < filename
Pipeprogram1 | program2Take everything from standard out of program1 and pass it to standard input of program2ls | more