next up previous contents index
Next: Filename Expansion Up: Using the Shell Previous: Aliases   Contents   Index


Controlling Input and Output

Throughout your experiences with Linux, you will most likely find that manipulating application input and output can be a very powerful thing to do. This section describes some of the things that controlling input and output can do for you.

stdin, stdout, Pipelines, and Redirection

Every process has at least three connections to the outside world. The standard input is one source of the process's data; the standard output is one place the process sends data; and the standard error is a place the process can send error messages. (These are often abbreviated stdin, stdout, and stderr.)

The words ``source'' and ``place'' are intentionally vague. These standard input and output locations can be changed by the user; they could be the screen, the keyboard, a file, even a network connection. You can specify which locations to use.

When you run a program from the shell, usually standard input comes from your keyboard, and standard output and error both go to your screen. However, you can ask the shell to change these defaults.

For example, the echo command sends it output to standard output, normally the screen. But you can send it to a file instead with the output redirection operator, >. For example, to put the word ``Hello'' in the file myfile, use this command:

echo Hello > myfile
Use cat or your text file pager (more or less) to view myfile's contents; see Figure 6.3 on page [*].

Figure 6.3: Redirecting output
\setlength {\rightmargin}{\leftmargin}\ra...
\par Hello

You can change the standard input of a command with the input redirection operator, <. For example, cat < myfile will display the contents of myfile. This is not useful in practice; for convenience, the cat command accepts a filename argument. So you can simply say cat myfile, and the effect will be the same. redirection operators

Under the hood, cat < myfile means that the shell opens myfile and then feeds its contents to the standard input of catcat myfile, without the redirection operator, means that the cat command receives one argument (myfile) opens the file itself, and then displays the file.

There's a reason for the double functionality, however. For example, you can connect the standard output of one command to the standard input of another. This is called a pipeline, and it uses the pipe operator6.1, |.

Perhaps you want to see the GNU General Public License in reverse. To do this, you use the tac command (it's cat, only backward). Try it out:

tac /usr/doc/copyright/GPL 
Unfortunately, it goes by too quickly to read. So you only get to see a couple of paragraphs. The solution is a pipeline:

tac /usr/doc/copyright/GPL | less
This takes the standard output of tac, which is the GPL in reverse, and sends it to the standard input of less.

You can chain as many commands together as you like. Say you have an inexplicable desire to replace every G with Q. For this you use the command tr G Q, like this:

tac /usr/doc/copyright/GPL | tr G Q | less 
You could get the same effect using temporary files and redirection, for example:

tac /usr/doc/copyright/GPL > tmpfile

tr G Q < tmpfile > tmpfile2

less < tmpfile2

rm tmpfile tmpfile2 

Clearly a pipeline is more convenient.

next up previous contents index
Next: Filename Expansion Up: Using the Shell Previous: Aliases   Contents   Index
John Goerzen / Ossama Othman