Understand InputOutput Streams


Understand Input/Output Streams

To take advantage of the information in this chapter, you need to understand that there are three input/output streams for a Linux shell: standard input, standard output, and standard error. Each of these streams has a file descriptor (or numeric identifier), a common abbreviation, and a usual default.

For instance, when you're typing on your keyboard, you're sending input to standard input, abbreviated as stdin and identified as 0. When your computer presents output on the terminal, that's standard output, abbreviated as stdout and identified as 1. Finally, if your machine needs to let you know about an error and displays that error on the terminal, that's standard error, abbreviated as stderr and identified as 2.

Let's look at these three streams using a common command, ls. When you enter ls on your keyboard, that's using stdin. After typing ls and pressing Enter, the list of files and folders in a directory appears as stdout. If you try to run ls against a folder that doesn't exist, the error message that appears on your terminal is courtesy of stderr.

Table 4.1 can help you keep these three streams straight.

Table 4.1. The Three Input/Output Streams

File Descriptor (Identifier)

Name

Common Abbreviation

Typical Default

0

Standard input

stdin

Keyboard

1

Standard output

stdout

Terminal

2

Standard error

stderr

Terminal


In this chapter, we're going to learn how to redirect input and output. Instead of having output appear on the terminal, for instance, you can redirect it to another program. Or instead of acquiring input from your typing, a program can get it from a file. After you understand the tricks you can play with stdin and stdout, there are many powerful things you can do.



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net