The shell and text tools
The shell as a loop that turns lines into processes: quoting and the order of expansions, pipes and redirections as file descriptors, exit codes and strict mode, log analysis with text tools, and when to switch to Python.
Silent failure by default
The previous topic described delivery as a stream of steps from a commit to production. A step becomes cheap when a machine can run it, and almost every step — building, testing, packaging, copying, restarting — is in the end a sequence of commands. The language of those commands is the shell.
The simplest automation is none: an engineer types commands from a wiki. Saving them in a file does not fix silence by itself. A three-line cleanup script without safeguards, run with an empty BUILD_DIR where the build directory is missing:
#!/usr/bin/env bash
cd /nonexistent/build
echo "still running, in ${PWD##*/}"
echo rm -rf "$BUILD_DIR/tmp"The cd fails and the script continues in the wrong directory. The empty variable turns $BUILD_DIR/tmp into /tmp. Without the echo, the script would try to delete /tmp. Both mistakes are silent by default.
Words, quoting and expansion
A shell is a read–evaluate–print loop whose values are processes. It reads a line, splits it into words, expands them, and runs the first word as a command — a builtin, or a program found on PATH via fork/exec.
Before running anything, bash turns a line into words through a fixed sequence (Bash manual §3.5):
- Brace expansion:
file{1..3}.log→ three words. - Tilde, parameter, arithmetic and command substitution, left to right.
- Word splitting of unquoted results of step 2 on
IFS(space, tab, newline by default). - Pathname expansion of unquoted
*,?,[…]. - Quote removal.
Single quotes stop every expansion. Double quotes allow step 2 (except tilde) but stop 3 and 4, so a variable stays one word. Most shell bugs are steps 3 and 4 applied to a value that should have stayed whole. With f='my report.txt', unquoted $f becomes two words; "$f" stays one. Habit: double-quote every $variable and $(command) unless splitting is intended; use "${array[@]}" and [[ … ]].
Pipes and redirection
Every process starts with three open file descriptors: 0 stdin, 1 stdout, 2 stderr. The shell decides what they point at before exec.
cmd > fileopensfileas descriptor 1;>>appends;< fileas 0.2> filefor stderr;2>&1makes 2 a copy of whatever 1 is at that moment (dup2).a | bcreates a pipe: write end as 1 ofa, read end as 0 ofb.
Order matters. ls … > all.txt 2>&1 sends both streams to the file. ls … 2>&1 > all.txt copies the terminal into 2 first, then redirects 1 — the error still reaches the terminal. A here-string <<< text feeds descriptor 0; process substitution <(cmd) passes a pipe name as a file argument.
Exit codes and strict mode
Every command returns a small integer status in $?: 0 means success, anything else failure. By default the shell continues after failure. The usual strict header for scripts is:
set -euo pipefail-e— exit when a simple command fails (with documented exceptions in conditionals).-u— treat unset variables as errors (catches$BUILD_DIRtypos).pipefail— a pipeline's status is the rightmost non-zero status, not only the last command. Without it,false | trueexits 0.
trap '…' EXIT and trap '…' ERR clean up or log on the way out. Still check commands whose failure is expected, and do not rely on -e alone inside $(…) in all bash versions the same way.
Text tools
Delivery steps often mean filtering logs and configs. A small toolkit covers most of it: grep/rg to select lines, cut/awk to pick fields, sort/uniq to aggregate, sed for substitutions, jq for JSON, xargs/find to turn lines into arguments. Compose them with pipes; keep each stage honest about exit status (grep returns 1 on no match — often fine with || true where absence is allowed).
When the script grows data structures, nested error handling, or parsers that are hard to test, switch to Python (or another general-purpose language) and keep the shell as the glue that launches it.
What changed
| Baseline | Habit | |
|---|---|---|
| Failure | continue after cd / empty vars |
set -euo pipefail, quote expansions |
| Words | unquoted $f splits and globs |
"$f", arrays, [[ … ]] |
| Streams | hope stderr is visible | know 2>&1 order; log both |
| Long logic | 200-line bash | short bash + Python |
Key idea. A shell program's statements are processes, its values are byte streams and exit statuses, and its syntax is decided by how a line is split into words. Quote expansions, connect programs through file descriptors, and check every exit status — or errors pass silently into the next command.
The next topic, Git: the object model and workflows, is the content-addressed history every build starts from.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.