Strict shell options turn common scripting mistakes into early failures. Defaults keep missing inputs explicit without weakening the script.

Program

Play the script to watch a missing argument become a default value, then become a filename-safe slug.

strict_mode.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

set -euo pipefail
input="${1:-draft report}"
safe="${input// /_}"
echo "$safe"
  1. options ← errexit, nounset, pipefail

    3set -euo pipefail4input="${1:-draft report}"
    values this steperrexit, nounset, pipefailoptions
  2. input ← draft report

    3set -euo pipefail4input="${1:-draft report}"5safe="${input// /_}"
    values this stepdraft reportinputunset$1
  3. safe ← draft_report

    4input="${1:-draft report}"5safe="${input// /_}"6echo "$safe"
    values this stepdraft_reportsafedraft reportinput
  4. echo "$safe"

    5safe="${input// /_}"6echo "$safe"
    outputdraft_report
    values this stepdraft_reportsafe

Follow the Script

  1. set -euo pipefail turns on errexit, nounset, and pipefail.
  2. $1 is unset in the traced run.
  3. ${1:-draft report} makes input equal draft report.
  4. ${input// /_} changes spaces to underscores.
  5. The script prints draft_report. | step | value | | --- | --- | | options | errexit, nounset, pipefail | | $1 | unset | | input | draft report | | safe | draft_report | | stdout | draft_report |
strict mode `set -euo pipefail` makes failed commands, unset variables, and failed pipelines visible.
default expansion `${1:-draft report}` supplies a value when the first argument is missing or empty.
substitution `${input// /_}` replaces every space in the value.

Exercise: strict_mode.sh

Reproduce draft_report, then trace how the unset first argument becomes the safe output.