graphics\overbutt.gifRedirecting input and output

Input is required before bash can execute the commands you enter. By default, bash reads standard input from the keyboard and writes command results, or standard output, to the computer screen.

You can redirect how bash reads standard input and where it writes standard output to using direction indicator keys. Use the less than sign (<) to redirect standard input and the greater than sign (>) to redirect standard output. For example, assume your working directory contains a file called fruit, which lists the names of different fruit.

kiwi banana orange peach pear apple

You can sort this list by redirecting standard input from the keyboard to the file, fruit. The shell writes the standard output to the screen.

Mabel~:$ sort < fruit

apple

banana

kiwi

orange

peach

pear

Instead of displaying the standard output, you can redirect it to a file. The following command redirects both standard input and standard output. The shell reads input from the file fruit, applies the Sort command to the file's contents, and puts the sorted list in a file called newfile.

Mabel~:$ sort < fruit > newfile

Redirecting output to an existing file overwrites the contents of that file. If the file does not already exist, bash creates it. You can use the direction indicators separately or in combination.

To redirect standard input from the keyboard to a file

1. At the command line, type,

<command> < <filename>

2. Press ENTER.

To redirect standard output from the computer screen to a file

1. At the command line, type,

<command> > <filename>

2. Press ENTER.

graphics\nicon.gif Notes