Unit 5 of 8 · about 4 hours · explain, implement

Shell streams, exit status, signals, processes and permissions

Every experiment runs through a shell. An unquoted path or a swallowed exit code turns a clean failure into silent corruption.

You'll be able to

  • Write a fail-fast script that quotes paths, validates inputs, propagates exit status and cleans up on signals.
  • Diagnose a failed process from exit status, streams, permissions, environment and signal behavior.
  • Explain quoting, redirection, pipelines, job control and partial-output recovery without trial and error.

Covers

Shell quoting, streams, exit status, signals and partial outputPermissions, environment, processes, file descriptors and logs
1

Read

In this order, only the parts listed

0 of 3 done

  1. Course shell: language, streams and strict modeThe Missing Semester of Your CS Education

    The shell language (bash): quoting, redirections, pipes, command substitution, shebang, set -euo pipefail and the run-until-failure script; Exercises on streams, exit status, scripts and permissions

  2. Command-line environment: streams, status and signalsThe Missing Semester of Your CS Education

    Arguments; Streams; Environment variables; Return codes; Signals; Exercises under Return Codes and Signals and Job Control

  3. Bash execution control and background logsThe Art of Debugging

    Bash: Controlling script execution (trace with -x, abort on failures inside the script and in the pipeline, abort on undefined variables, temporarily turning off set commands); nohup

2

Focus on

  • Quote every expansion and keep globs unquoted only when intended. Single quotes preserve literally; double quotes allow expansion.
  • Use set -euo pipefail, trap cleanup on EXIT, and make INT and TERM exit (130 and 143) so the EXIT trap runs. Without pipefail, false | true exits 0 and the failure vanishes.
  • Streams are separate contracts: stdout carries data, stderr carries diagnostics, the exit code carries the outcome. Redirect and check each one deliberately.
  • Background jobs report through $!, wait and kill. Partial output stays recoverable when each stage writes its own log.
Common mistakes · 3
  • set -e does not fire inside if conditions, && lists, or pipelines without pipefail. Test failure handling explicitly instead of assuming strictness.
  • Unquoted variables and command substitutions split on spaces. Filenames with spaces become several arguments, so quote or use arrays.
  • A cleanup trap on TERM that does not exit. Bash runs it only after the foreground command finishes, then resumes the script, which can exit 0; trap EXIT for cleanup, exit from the INT and TERM traps, and wait on background work so the signal lands at once.
3

Try it yourself

Closed-book, before checking anything

Write check-and-run.sh with a shebang, set -euo pipefail, a quoted $1 filename check with test -f, a temp file removed by trap on EXIT, traps that exit 130 on INT and 143 on TERM, a sleep run as sleep & wait $!, and separate stdout and stderr logs. Show that false | true exits 1 under pipefail and 0 without it on GNU bash 5.3.9, and that kill -TERM ends the script at once with exit code 143 and the temp file gone. Then drop the exit traps and watch TERM wait for the sleep and exit 0.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.false | true; echo $? prints what with and without pipefail?Answer

    1 with pipefail, 0 without. The pipeline status is the last command unless pipefail propagates the failure.

  2. 2.Why must cd be a shell builtin?Answer

    A child process cannot change its parent working directory, so cd must run in the shell itself.

  3. 3.A script cleans up on Ctrl-C but not on kill -9. Why?Answer

    SIGKILL cannot be caught or ignored. Traps handle INT and TERM but never KILL, so temp files must tolerate it.

  4. 4.Output still appears after cmd 2>/dev/null. What happened?Answer

    Usually the text is on stdout, not stderr. Otherwise another pipeline stage printed it, or the program writes to /dev/tty directly, as password prompts do.

5

Practice

You're done when

Your script handles a filename with spaces, reports the right exit code for missing input, and leaves no temp files after SIGTERM.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 5 more in the section
  • The Missing Semester of Your CS EducationMIT · primary

    Complete the 2026 lectures on the shell, command-line environment, development tools, debugging and profiling, Git, packaging and shipping code, and code quality. Apply every exercise to the same stage project.

  • The Art of DebuggingStas Bekman · support

    Use the Unix, Python and compiled-code sections during a real seeded defect. Follow the evidence and tiny-reproducer workflow rather than reading the unfinished book front to back.

Also in this section

  • pytest documentationpytest · required

    Use Get Started, fixtures, parametrization, monkeypatch, caplog and temporary paths to build behavior and failure-path tests without order dependence.

  • Hypothesis documentationHypothesis · required

    Follow the Quick start and property-based testing guides, then generate operation histories and retain the minimized example for one real defect.

  • Pro GitGit project · support

    Use Chapters 2-3 and the sections on debugging, internals and recovery while constructing a history for bisect and recovering a deliberately displaced commit.

  • Stas' Python Cookbook - debugging and testingStas Bekman · support

    Use Part III and the packaging sections for debugger, traceback, pytest, logging, profiling and clean-environment recipes.

  • AlgoMaster Python - mockingAlgoMaster · support

    Use the Mocking lesson for unittest.mock: Mock versus MagicMock, patch targets, autospec and the over-mocking trap. The pytest docs remain the reference for fixtures and monkeypatch.