Small command-line options let callers change behavior without editing the script body.

Program

Play the script to choose an option and see how the interface maps it to a mode.

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

option="--dry-run"
case "$option" in
    --dry-run) mode="preview" ;;
    --force) mode="apply" ;;
esac
echo "mode=$mode"
#!/usr/bin/env bash

option="--force"
case "$option" in
    --dry-run) mode="preview" ;;
    --force) mode="apply" ;;
esac
echo "mode=$mode"
  1. option ← --dry-run

    3option="--dry-run"4case "$option" in
    values this step--dry-runoption
  2. case "$option" in

    3option="--dry-run"4case "$option" in5    --dry-run) mode="preview" ;;
    values this step--dry-runoption
  3. mode ← preview

    4case "$option" in5    --dry-run) mode="preview" ;;6    --force) mode="apply" ;;
    values this steppreviewmode
  4. echo "mode=$mode"

    7esac8echo "mode=$mode"
    outputmode=preview
    values this steppreviewmode
  1. option ← --force

    3option="--force"4case "$option" in
    values this step--forceoption
  2. case "$option" in

    3option="--force"4case "$option" in5    --dry-run) mode="preview" ;;
    values this step--forceoption
  3. mode ← apply

    5    --dry-run) mode="preview" ;;6    --force) mode="apply" ;;7esac
    values this stepapplymode
  4. echo "mode=$mode"

    7esac8echo "mode=$mode"
    outputmode=apply
    values this stepapplymode
option A leading-dash option is a compact caller-facing control.
case parse `case` maps recognized options to internal behavior.
mode Using an internal mode word keeps the rest of the script independent of option spelling.