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, as seen in the following sections.

Simple redirection

cmd > file

Send output of cmd to file (overwrite).

cmd >> file

Send output of cmd to file (append).

cmd < file

Take input for cmd from file.

cmd << text

The contents of the shell script up to a line identical to text become the standard input for cmd (text can be stored in a shell variable). This command form is sometimes called a Here document. Input is usually typed at the keyboard or in the shell program. Commands that typically use this syntax include cat, ex, and sed. (If <<- is used, leading tabs are stripped from the contents of the here document, and the tabs are ignored when comparing input with the end-of-input text marker.) If any part of text is quoted, the input is passed through verbatim. Otherwise, the contents are processed for variable, command, and arithmetic substitutions.

cmd <<< word

Supply text of word, with trailing newline, as input to cmd. (This is known as a here string, from the free version of the rc shell.)

cmd <> file

Open file for reading and writing on the standard input. The contents are not destroyed.[7]

cmd >| file

Send output of cmd to file (overwrite), even if the shell’s noclobber option is set.

Redirection using file descriptors

Syntax

Effect

cmd >& n

Send cmd output to file descriptor n.

cmd m>& ...

Get Linux in a Nutshell, 6th Edition 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.