Linux Command Reference ยท Bash Syntax
Bash redirection
Reference for Bash redirection: send output to files, read input from files, redirect stderr, merge streams with 2>&1, open descriptors, noclobber and /dev/null.
Every process starts with three open channels called file descriptors: 0 is standard input, 1 is standard output and 2 is standard error. Redirection changes where those channels point before the command runs, so output can go to a file, input can come from a file, and errors can be separated from normal output.
The shell sets up redirections from left to right, which explains why 2>&1 is so sensitive to its position on the line. The program itself never sees the operators.
cmd > out.txt # stdout to a file (overwrite) cmd >> out.txt # append cmd < in.txt # stdin from a file cmd > all.txt 2>&1 # stdout and stderr to the same file
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
[n]> file [n]>> file [n]< file [n]>&m [n]<&m [n]>&- [n]<&- &> file &>> file [n]<> file >| file
| Operator | Description |
|---|---|
> file |
Send standard output to file, creating or truncating it. |
>> file |
Append instead of truncating. |
< file |
Read standard input from file. |
n> file |
The same for descriptor n; 2> file redirects errors. |
n>&m |
Make descriptor n a copy of m. 2>&1 points stderr to wherever stdout points now. |
n>&- |
Close descriptor n. |
&> file, &>> file |
Bash shortcut for > file 2>&1 (and its append form). |
<> file |
Open file for reading and writing. |
>| file |
Overwrite even when noclobber is set. |
<<, <<< |
Here-document and here-string; see Here-documents. |
Returns: the command's own status; a failed redirection (missing file, no permission) prints an error and gives status 1 without running the command.
Errors / edge cases: a redirection is performed even if the command is not found, so nosuchcmd > f still creates an empty file f.
Usage notes
- Left to right.
cmd > f 2>&1sends both to the file;cmd 2>&1 > fsends errors to the old stdout (usually the terminal) and only stdout to the file. - Never read and write the same file in one command (
sort f > f). The shell truncatesfbeforesortstarts. Write to a temporary file, thenmv; or usespongeorsort -o f f. /dev/nullswallows anything written and returns end-of-file on read.cmd >/dev/null 2>&1is the classic silence idiom.- Pipes carry only stdout. To pipe stderr as well, use
cmd 2>&1 | otherorcmd |& otherin Bash. - Redirections apply to compound commands too:
{ a; b; } > out,while ...; done < file,( ... ) 2>err. - Permanent changes use
exec:exec > log 2>&1. Seeexec. - Descriptor numbers above 9 are possible via
exec {fd}> file, where Bash picks a free number and stores it infd. - Special files: Bash emulates
/dev/stdin,/dev/stdout,/dev/stderr,/dev/tcp/host/portand/dev/udp/host/porteven if the system lacks them. - Spaces:
2> fileis stderr, but2 > filepasses2as an argument. No space between the number and the operator. - Append races:
>>is atomic for small writes, which is why logs written with>>from several processes mostly stay line-intact.
Related
- Here-documents and here-strings, Pipelines, Process substitution,
tee,exec - Lessons: Streams, Pipes, Here-docs