Next: 3.8.3 Using pipes Up: 3.8 UNIX Plumbing Previous: 3.8.1 Standard input and

## 3.8.2 Redirecting input and output

Now, let's say that we wanted to send the output of `sort` to a file, to save our shopping list elsewhere. The shell allows us to redirect standard output to a filename, using the ```>`'' symbol. Here's how it works.

`/home/larry/papers# sort > shopping-list
bananas
carrots
apples
/home/larry/papers#`

As you can see, the result of the `sort` command isn't displayed, instead it's saved to the file `shopping-list`. Let's look at this file.

`/home/larry/papers# cat shopping-list
apples
bananas
carrots
/home/larry/papers#`

Now we can sort our shopping list, and save it, too! But let's suppose that we were storing our unsorted, original shopping list in the file `items`. One way of sorting the information and saving it to a file would be to give `sort` the name of the file to read, in lieu of standard input, and redirect standard output as we did above. As so:

`/home/larry/papers# sort items > shopping-list
/home/larry/papers# cat shopping-list
apples
bananas
carrots
/home/larry/papers#`

However, there's another way of doing this. Not only can we redirect standard output, but we can redirect standard input as well, using the ```<`'' symbol.

`/home/larry/papers# sort < items
apples
bananas
carrots
/home/larry/papers#`

Technically, `sort < items` is equivalent to `sort items`, but the former allows us to demonstrate the point: `sort < items` behaves as if the data in the file `items` was typed to standard input. The shell handles the redirection. `sort` wasn't given the name of the file (`items`) to read; as far as `sort` is concerned, it was still reading from standard input as if you had typed the data from your keyboard.

This introduces the concept of a filter. A filter is a program which reads data from standard input, processes it in some way, and sends the processed data to standard output. Using redirection, standard input and/or standard output can be referenced from files. `sort` is a simple filter: it sorts the incoming data and sends the result to standard output. `cat` is even simpler: it doesn't do anything with the incoming data, it simply outputs whatever was given to it.

Next: 3.8.3 Using pipes Up: 3.8 UNIX Plumbing Previous: 3.8.1 Standard input and