Notes
What the shell does
When you press enter, the shell reads the line, expands variables, wildcards and command substitutions, sets up any redirections and pipes, and starts each program, using the fork and exec system calls from episode one. Then it waits, and stores the program's exit code. A few commands, like cd and export, are builtins that run inside the shell itself, because they must change the shell's own state.
- expand: $VAR, *, ~, $(cmd)
- set up redirections and pipes
- fork a child, exec the program
- wait for it to finish
- save its exit code in $?
- builtins (cd, export) run inside the shell itself
Three streams
Every process starts with three open file descriptors. Zero is standard input, normally the keyboard. One is standard output, for normal results. Two is standard error, for error messages and diagnostics. By default all three are connected to your terminal. Keeping errors on a separate stream is what lets you save results to a file while still seeing errors on screen.
- 0 stdin input (keyboard)
- 1 stdout normal output
- 2 stderr errors and logs
- all three go to the terminal by default
- they are just file descriptors (episode 1)
Redirection
Redirection points a stream somewhere else. Greater than sends standard output to a file, replacing it; two greater thans append. Two greater than redirects only errors. To capture both, redirect output to a file and then two greater than ampersand one, which means send errors wherever output now goes. The order matters. Less than reads input from a file, and slash dev slash null swallows anything you do not want.
cmd > out.txt stdout, replace cmd >> out.txt stdout, append cmd 2> err.log stderr only cmd > all.log 2>&1 both cmd < in.txt stdin from file cmd 2>/dev/null drop errors order matters: 2>&1 after >
Pipes
A pipe connects one program's standard output to the next program's standard input. Both run at the same time, with the kernel holding a small buffer between them. We piped ten million numbers from seq into wc minus l, which counts lines. It printed ten million, in a hundred and fourteen milliseconds. Nothing touched the disk. This is how one line of shell can search, filter, count and sort gigabytes of logs.
$ time (seq 10000000 | wc -l) 10000000 real 0m0.114s both programs run at the same time the kernel buffers between them (64 KB by default)
Exit codes
Every program finishes with an exit code. Zero means success, anything from one to two hundred and fifty five means some kind of failure. ls on a missing path returned two in our run. A few codes are worth memorising: one twenty seven means command not found, one twenty six means not executable, one thirty seven means killed by signal nine, and one forty three means stopped by signal fifteen. CI systems, scripts and Kubernetes all decide what to do from this number.
$ ls /nope; echo $? 2 $ true; echo $? → 0 $ false; echo $? → 1 126 found but not executable 127 command not found 137 killed by SIGKILL (128+9) 143 stopped by SIGTERM (128+15)
The pipeline trap
Now the trap. A pipeline's exit code is only the last command's. False piped into true returned zero in our test, so the failure disappeared. With the option pipefail switched on, the same pipeline returned one. This matters in CI. A curl that fails, piped into jq, can still show green. That is why careful scripts start with strict settings, which part three covers properly.
$ false | true; echo $? 0 ← the failure hid $ set -o pipefail $ false | true; echo $? 1 curl -s url | jq . passes even when curl fails, unless pipefail is on
Chaining
Exit codes also drive chaining. Double ampersand runs the next command only if the last one succeeded. Double bar runs it only if the last one failed. A semicolon runs it regardless. Dollar brackets inserts a command's output into another command, and a single ampersand runs something in the background. Make a folder and go into it, or stop the script: that one line protects you from running commands in the wrong place.
a && b b only if a succeeds a || b b only if a fails a ; b b always $(cmd) insert cmd's output cmd & run in background mkdir -p /opt/app && cd /opt/app || exit 1
Common mistakes
Mistakes. Writing two greater than ampersand one before the file redirection, which sends errors to the terminal instead. Ignoring exit codes. Pipelines without pipefail in scripts. Redirecting into the same file you are reading, which empties it before the command starts. Variables without quotes, which break on spaces. And parsing the output of ls, which breaks on unusual file names.
- cmd 2>&1 > file (wrong order)
- ignoring exit codes
- pipelines without pipefail
- > on the file you are reading (it is emptied first)
- unquoted "$VAR" with spaces
- parsing ls output
Say it like this
In the interview: every process starts with standard input, output and error on descriptors zero, one and two. Two greater than ampersand one makes standard error go wherever standard output currently points, so command greater than log, two greater than ampersand one, captures both. A pipe connects one program's output to the next one's input, and since a pipeline returns only the last exit code, scripts set pipefail.
- "Processes start with stdin 0, stdout 1 and stderr 2. 2>&1
- sends stderr where stdout points, so cmd > log 2>&1 captures both.
- A pipe joins stdout to stdin. A pipeline returns the last exit
- code, so scripts set pipefail."