Every running program under Unix starts with three files already opened
Table 1-1. Standard Files Provided by Unix
Descriptive Name | File Number | Description |
---|---|---|
Standard In | 0 | Input from the keyboard |
Standard Out | 1 | Output to the console |
Standard Error | 2 | Error 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.
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.
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
Name | Command | Description | Example |
---|---|---|---|
Redirect to a file | > filename | Take 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 | < filename | Copy all data from the file to the standard input of the program | echo < filename |
Pipe | program1 | program2 | Take everything from standard out of program1 and pass it to standard input of program2 | ls | more |