Debugging Scripts
Debug Flag
Choose Verbose Mode
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_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"
debug ← 0
3debug=04if [[ "$debug" -eq 1 ]]; thenvalues this step0debugif [[ "$debug" -eq 1 ]]; then
3debug=04if [[ "$debug" -eq 1 ]]; then5 mode="verbose"values this step0debugmode ← normal
6else7 mode="normal"8fivalues this stepnormalmodeecho "debug:$mode"
8fi9echo "debug:$mode"outputdebug:normalvalues this stepnormalmode
debug ← 1
3debug=14if [[ "$debug" -eq 1 ]]; thenvalues this step1debugif [[ "$debug" -eq 1 ]]; then
3debug=14if [[ "$debug" -eq 1 ]]; then5 mode="verbose"values this step1debugmode ← verbose
4if [[ "$debug" -eq 1 ]]; then5 mode="verbose"6elsevalues this stepverbosemodeecho "debug:$mode"
8fi9echo "debug:$mode"outputdebug:verbosevalues 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.