Redirection Forms

Table 5-20. I/O file descriptors

File descriptor

Name

Common abbreviation

Typical default

0

Standard input

stdin

Keyboard

1

Standard output

stdout

Screen

2

Standard error

stderr

Screen

The usual input source or output destination can be changed as shown in Table 5-21.

Table 5-21. I/O redirectors

Redirector

Function

> file

Direct standard output to file.

< file

Take standard input from file.

cmd1 | cmd2

Pipe; take standard output of cmd1 as standard input to cmd2.

>> file

Direct standard output to file; append to file if it already exists.

>| file

Force standard output to file even if noclobber is set.

n>| file

Force output from the file descriptor n to file even if noclobber is set.

<> file

Use file as both standard input and standard output.

<< text

Read standard input up to a line identical to text (text can be stored in a shell variable). Input is usually typed on the screen or in the shell program. Commands that typically use this syntax include cat, echo, ex, and sed. If text is enclosed in quotes, standard input will not undergo variable substitution, command substitution, etc.

n> file

Direct file descriptor n to file.

n< file

Set file as file descriptor n.

>&n

Duplicate standard output to file descriptor n.

<&n

Duplicate standard input from file descriptor n.

&>file

Direct standard output and standard error to file.

<&-

Close the standard input.

>&-

Close the standard output.

n>&-

Close ...

Get Mac OS X Tiger in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.