Configuration precedence and CLI testing
Configuration precedence decides which setting wins when several sources supply values. Choose and document an order, for example explicit CLI option > environment variable > parsed configuration file > built-in default. This lesson uses a fixed synthetic config value, not a real file parser. Treat a configuration file as data; do not source an untrusted file. Decide how empty values differ from absent values: ${BM_MODE-$default} uses the default only if the variable is unset, whereas ${BM_MODE:-$default} also replaces an empty value. Validate the final selected mode against a fixed allowed set. Test every source independently and combined, including repeated flags, empty values, missing values, and invalid modes. Assert stdout, stderr, status and side effects separately. Avoid printing secrets in logs; a version or progress report should not expose credentials. The final example compares expected and actual output files byte for byte with cmp; command substitution strips trailing newlines and would miss such a difference. The synthetic config source is a scalar, not a parsed file.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Layer defaults and explicit values
resolve() { local default=dev config mode config=${BM_CONFIG-$default} mode=${BM_MODE-$config} if (( $# )); then if (( $# != 2 )) || [[ $1 != --mode ]]; then return 2; fi mode=$2 fi case $mode in dev|test|prod) printf '%s\n' "$mode" ;; *) printf 'invalid mode\n' >&2; return 2 ;; esac}unset BM_MODE BM_CONFIGresolveBM_CONFIG=testresolveBM_MODE=prod resolveBM_MODE=prod resolve --mode devExpected stdout:
devtestproddevThe absent synthetic config falls back to dev. A config value overrides the default; environment overrides config and CLI overrides environment.
Absent versus explicitly empty
unset BM_MODEprintf '[%s]\n' "${BM_MODE-dev}"BM_MODE=''printf '[%s]\n' "${BM_MODE-dev}" "${BM_MODE:-dev}"Expected stdout:
[dev][][dev]The minus-only form keeps an explicitly empty value for later validation.
Assert a fixed CLI contract
mode_cli() { local mode=${BM_MODE-dev} if (( $# )); then if (( $# != 2 )) || [[ $1 != --mode ]]; then return 2; fi mode=$2 fi case $mode in dev|test|prod) printf '%s\n' "$mode" ;; *) printf 'invalid mode\n' >&2; return 2 ;; esac}BM_MODE=prod mode_cli --mode test > actual.txt 2> error.txtstatus=$?printf 'test\n' > expected.txt || exit 1[[ $status == 0 ]] && cmp -s expected.txt actual.txt && [[ ! -s error.txt ]] || exit 1printf 'PASS\n' Expected stdout:
PASSThis checks status, byte-exact stdout (including the final newline) and empty stderr for one case.
Common mistakes
Do not silently interpret an explicit empty option as absent unless the contract says so. Do not source arbitrary configuration or concatenate user values into executable code.
Practice
Write a mode resolver with default dev, optional config test, environment BM_MODE and --mode. Define empty as invalid. Test every precedence level and invalid/empty values; capture status and compare the expected output independently.
Next: Error handling, shell options and traps