Redirection & Pipes
Streams & Redirection
Every process has three standard streams:
| Stream | FD | Default |
|---|
| stdin | 0 | keyboard |
| stdout | 1 | terminal |
| stderr | 2 | terminal |
| Operator | Effect |
|---|
> | Redirect stdout (overwrite) |
>> | Redirect stdout (append) |
2> | Redirect stderr |
&> | Redirect stdout and stderr |
2>&1 | Send stderr to wherever stdout goes |
< | Redirect stdin from a file |
| `\ | ` | Pipe stdout into another command's stdin |
tee | Write to a file and pass through |
$ command > out.log 2>&1 # both streams to one file
$ ls /nope 2> /dev/null # discard errors
$ dmesg | grep -i error | tee errors.txt | wc -l
Exam trap: Order matters - > file 2>&1 works, but 2>&1 > file sends stderr to the old stdout (terminal), then only stdout to the file.