Redirection Forms

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 with the redirection commands listed in the following sections.

Simple redirection

Command

Action

cmd > file

Send output of cmd to file (overwrite).

cmd >! file

Same as preceding, even if noclobber is set.

cmd >> file

Send output of cmd to file (append).

cmd >>! file

Same as preceding, even if noclobber is set.

cmd < file

Take input for cmd from file.

cmd << text

Read standard input up to a line identical to text (text can be stored in a shell variable). Input usually is typed at the keyboard or in the shell program. Commands that typically use this syntax include cat, echo, ex, and sed. If text is quoted (using any of the shell’s quoting mechanisms), the input is passed through verbatim. Otherwise, the shell performs variable and command substitutions on the input. When quoting text, the ending delimiter must be quoted identically.

Multiple redirection

Command

Action

cmd >& file

Send both standard output and standard error to file.

cmd >&! file

Same as preceding, even if noclobber is set.

cmd >>& file

Append standard output and standard error to end of file.

cmd >>&! file

Same as preceding, even if noclobber is set.

cmd1 |& cmd2

Pipe standard error together with standard output.

( cmd > f1 ...

Get Unix in a Nutshell, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.