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 |
|---|---|
| Send
|
|
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access