Many shell scripts use a flag to decide whether to print normal output or extra diagnostic detail.

Program

Play the script to choose debug mode and see the planned diagnostic output.

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

debug=0
if [[ "$debug" -eq 1 ]]; then
    mode="verbose"
else
    mode="normal"
fi
echo "debug:$mode"
#!/usr/bin/env bash

debug=1
if [[ "$debug" -eq 1 ]]; then
    mode="verbose"
else
    mode="normal"
fi
echo "debug:$mode"
  1. debug ← 0

    3debug=04if [[ "$debug" -eq 1 ]]; then
    values this step0debug
  2. if [[ "$debug" -eq 1 ]]; then

    3debug=04if [[ "$debug" -eq 1 ]]; then5    mode="verbose"
    values this step0debug
  3. mode ← normal

    6else7    mode="normal"8fi
    values this stepnormalmode
  4. echo "debug:$mode"

    8fi9echo "debug:$mode"
    outputdebug:normal
    values this stepnormalmode
  1. debug ← 1

    3debug=14if [[ "$debug" -eq 1 ]]; then
    values this step1debug
  2. if [[ "$debug" -eq 1 ]]; then

    3debug=14if [[ "$debug" -eq 1 ]]; then5    mode="verbose"
    values this step1debug
  3. mode ← verbose

    4if [[ "$debug" -eq 1 ]]; then5    mode="verbose"6else
    values this stepverbosemode
  4. echo "debug:$mode"

    8fi9echo "debug:$mode"
    outputdebug:verbose
    values this stepverbosemode
debug flag A small numeric flag can switch a script between normal and verbose behavior.
diagnostic mode Verbose mode should reveal decisions without changing the real result.
branch The `if` statement copies the selected behavior into one output variable.