Bash: Data Streams
https://www.gnu.org/software/bash/manual/bash.html#Redirections
Streams
Standard input (stdin), standard output (stdout), and standard error (stderr).
File Descriptors
| File Descriptor | Name |
|---|---|
| 0 | stdin |
| 1 | stdout |
| 2 | stderr |
Redirecting stdout and stderr
| Description | Command |
|---|---|
Redirect stdout to FILE. |
COMMAND > FILE |
| Same as above. | COMMAND 1> FILE |
Redirect stderr to FILE. |
COMMAND 2> FILE |
Redirect stdout to FILE1 and stderr to FILE2. |
COMMAND 1> FILE1 2> FILE2 |
Redirect stdout and stderr to the same FILE. |
COMMAND &> FILE |
2>&1 instructs the shell to redirect stderr (2) to the same destination as stdout (1). 2>&1 must come after FILE. &> is a Bash-ism. |
COMMAND > FILE 2>&1 |
Input streams can be piped to /dev/null to get rid of them. |
COMMAND > /dev/null |
| Persistently re-direct stderr to FILE until the shell or script closes, or the command is overridden. | exec 2> FILE |
Appending stdout and stderr
In all the commands above, a single > can become >> to trigger an append to FILE, rather than
an overwrite. 2>&1 remains the same; only the preceding > needs to be changed to >>.
Taking input from sources other than stdin
COMMAND < FILE
< causes COMMAND to take input from FILE rather than stdin.
COMMAND «< WORD
WORD undergoes tilde expansion, parameter and variable expansion, command substitution, arithmetic
expansion, and quote removal. Path name expansion and word splitting are not performed. This
operator is called Here Strings.
Here Documents
Redirect multiple lines to a command.
COMMAND « TERMINATOR
After pressing enter, the shell will allow extra lines to be input. Every line will be redirected
to COMMAND until a line contains TERMINATOR. TERMINATOR lines are not redirected to COMMAND.
COMMAND «- TERMINATOR
Same as above except leading tab characters are stripped from input lines.
Explanation
If the initial TERMINATOR is not quoted, all the lines input will be subject to parameter
expansion, command substitution, and arthimetic expansion. \, $, and \ must be escaped with
`, and new lines character are ignored.
If the initial TERMINATOR is quoted, no expansion will occur. The second occurence of TERMINATOR
should not be quoted.
noclobber
By default, standard distros ship with an option called noclobber off. With noclobber on, the
> operator will not overwrite existing files and will instead return an error. The >|
operator can be used to override this behaviour and force an overwrite. noclobber has no impact
on the >> operator.
| Description | Command |
|---|---|
Turn on noclobber. |
set -o noclobber |
Turn off noclobber. |
set +o noclobber |
Gotchas
COMMAND >& FILE:
Another alternative toCOMMAND &> FILEfor redirecting both stdout a nd stderr. Bad practice. FILE may not expand to a number or-.
To do
- Custom file descriptors.
- Moving file descriptors.
- Opening file descriptors for reading and writing.