Pipes & Redirection
Every program started from Bash gets three standard streams: it reads from standard input and writes normal output to standard output and error messages to standard error. Redirection connects these streams to files, and pipes connect one program's output to the next program's input. This is the core idea of the Unix philosophy: small tools, combined.
| Stream | Number | Default |
|---|---|---|
| standard input | 0 | the keyboard |
| standard output | 1 | the terminal |
| standard error | 2 | the terminal |
Output redirection: > and >>
echo "first run" > status.txt
echo "second run" > status.txt
cat status.txt
echo "line A" >> history.txt
echo "line B" >> history.txt
cat history.txtsecond run
line A
line B
> creates or truncates the file; >> appends. set -o noclobber makes > refuse to overwrite existing files (>| forces it).
Errors go to a different stream
Error messages are written to stream 2, so > alone doesn't capture them:
ls report.txt missing.txt > out.txt
echo "--- out.txt contains:"
cat out.txt
touch report.txt
ls report.txt missing.txt > out.txt 2> err.txt
echo "--- out.txt:"; cat out.txt
echo "--- err.txt:"; cat err.txtls: cannot access 'report.txt': No such file or directory
ls: cannot access 'missing.txt': No such file or directory
--- out.txt contains:
--- out.txt:
report.txt
--- err.txt:
ls: cannot access 'missing.txt': No such file or directory
The first time, report.txt didn't exist either, so both messages went to the screen and out.txt stayed empty.
| Syntax | Meaning |
|---|---|
cmd > file |
stdout to file |
cmd 2> file |
stderr to file |
cmd > file 2>&1 |
both to the same file (order matters!) |
cmd &> file |
both to the same file (Bash shorthand) |
cmd 2> /dev/null |
discard errors |
cmd > /dev/null 2>&1 |
discard everything |
/dev/null is a special file that swallows everything written to it.
2>&1 and why order matters
2>&1 means "make stream 2 go wherever stream 1 goes right now". Redirections are processed left to right:
touch present.txt
ls present.txt absent.txt > both.txt 2>&1
echo "--- both.txt:"; cat both.txt
ls present.txt absent.txt 2>&1 > only-out.txt
echo "--- only-out.txt:"; cat only-out.txt--- both.txt:
ls: cannot access 'absent.txt': No such file or directory
present.txt
ls: cannot access 'absent.txt': No such file or directory
--- only-out.txt:
present.txt
In the second command, stream 2 was pointed at the terminal (where stream 1 was at that moment) before stream 1 moved to the file, so the error still appeared on screen.
Input redirection: <, <<<
< feeds a file to a command's standard input. <<< (a here-string) feeds a single string:
printf 'charlie\nalpha\nbravo\n' > names.txt
sort < names.txt
wc -w <<< "three little words"
read -r first rest <<< "Ada Lovelace, mathematician"
echo "first=$first rest=$rest"alpha
bravo
charlie
3
first=Ada rest=Lovelace, mathematician
Here-documents (<< EOF) feed several lines; they get their own lesson.
Pipes
cmd1 | cmd2 connects the standard output of cmd1 to the standard input of cmd2. Both run at the same time, and data flows through without temporary files:
cat > access.log <<'LOG'
GET /home 200
GET /cart 500
POST /cart 200
GET /home 200
GET /admin 403
GET /home 200
LOG
grep -v ' 200