Redirection & Pipes

Streams & Redirection

Every process has three standard streams:

StreamFDDefault
stdin0keyboard
stdout1terminal
stderr2terminal
OperatorEffect
>Redirect stdout (overwrite)
>>Redirect stdout (append)
2>Redirect stderr
&>Redirect stdout and stderr
2>&1Send stderr to wherever stdout goes
<Redirect stdin from a file
`\`Pipe stdout into another command's stdin
teeWrite 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.