This chapter is what makes the command line powerful.
The Plain Picture#
A factory line: one machine cuts, the next paints, the next packs. Each machine does one simple job, and a conveyor belt carries the output of one straight into the next. By arranging simple machines differently, you can build almost any product.
The Real Names#
Every process has three standard streams (Operating Systems Ch 10): stdin (0), stdout (1), stderr (2). By default all three connect to your terminal. The shell can reconnect them:
| Syntax | Meaning |
|---|---|
cmd > file | send stdout to a file (overwrite it) |
cmd >> file | send stdout to a file (append to the end) |
cmd 2> errors.txt | send stderr to a file |
cmd > out.txt 2>&1 | send stdout to a file, and stderr to the same place as stdout |
cmd < input.txt | read stdin from a file |
cmd1 | cmd2 | a pipe: stdout of cmd1 becomes stdin of cmd2 — the conveyor belt |
cmd > /dev/null | throw output away (/dev/null is a device that discards everything written to it) |
The Unix idea behind it: small tools that each do one thing well, and read and write plain text, so any tool can feed any other.
Under the Hood#
A pipe is a small buffer in the kernel. Both commands run at the same time as separate processes; the first writes into the buffer, the second reads from it. If the reader is slow, the buffer fills and the writer is paused (blocked, Operating Systems Ch 3) — so pipes work even on files far bigger than RAM.
┌────────┐ stdout ┌─────────────┐ stdin ┌────────┐ stdout ┌──────────┐ stdin ┌────────┐ │ cat │ ───────▶ │ kernel pipe │ ──────▶ │ grep │ ───────▶ │ pipe │ ─────▶ │ wc -l │ ──▶ terminal └────────┘ └─────────────┘ └────────┘ └──────────┘ └────────┘
Exit codes join commands#
Every command exits with a code: 0 = success (Operating Systems Ch 12). The shell uses it:
echo $? # the exit code of the last command
mkdir build && cd build # && : run the second only if the first SUCCEEDED
cd /nonexistent || echo "no such dir" # || : run the second only if the first FAILED
cmd1 ; cmd2 # ; : run both, regardlessGotchas
>truncates the file before the command even runs.sort data.txt > data.txtgives you an empty file: the shell emptieddata.txtbeforesortread it. Write to a new file, thenmvit.The order in
> file 2>&1matters.2>&1 > filesends stderr to the terminal (where stdout was at that moment) and only stdout to the file.A pipeline's exit code is the last command's by default, so failures early in a pipe are hidden. Scripts fix this with
set -o pipefail(Ch 9).
Check yourself
5 questions> vs >>?
Show answerHide answer
> overwrites the file; >> appends.
How do you run a command and save both its normal output and its errors to run.log?
Show answerHide answer
cmd > run.log 2>&1.
What does | do?
Show answerHide answer
Connects the first command's stdout to the second command's stdin.
What does make build && ./deploy.sh guarantee?
Show answerHide answer
./deploy.sh runs only if make build succeeded (exit code 0).
Why does sort names.txt > names.txt destroy the file?
Show answerHide answer
The shell truncates names.txt to empty before sort reads it.