[This introduction is general and simplified. If you're a technical person who needs a complete and exact description, read a book on UNIX programming. -JP]
When the UNIX kernel starts any - for example, grep , ls , or a shell - it sets up several places for that process to read from and write to. Figure 45.1 shows that.
These places are called open files . The kernel gives each file a number called a file descriptor . But people usually use names for these places instead of the numbers:
By default, as Figure 45.1 shows, the file that's opened for stdin , stdout , and stderr is /dev/tty -a name for your terminal. This makes life easier for users - and programmers, too. The user doesn't have to tell a program where to read or write because the default is your terminal. A programmer doesn't have to open files to read or write from (in many cases); the programs can just read from stdin , write to stdout , and send errors to stderr .
This gets better. When the shell starts a process (when you type a command at a prompt), you can tell the shell what file to "connect to" any of those file descriptors. For example, Figure 45.2 shows what happens when you run grep and make the shell redirect grep 's standard output away from the terminal to a file named grepout .
Programs can read and write files besides the ones on stdin , stdout , and stderr . For instance, in Figure 45.2 , grep opened the file somefile itself - it didn't use any of the standard file descriptors for somefile . A UNIX convention is that if you don't name any files on the command line, a program will read from its standard input. Programs that work that way are called .
All shells can do basic redirection with stdin , stdout , and stderr . But, as you'll see in article 45.21 , the Bourne shell also handles file descriptors 3 through 9. That's useful sometimes: