Interview Prep/Shell Scripting/Scenarios: writing and hardening scripts

You are asked to add `set -e` to a large legacy script that has run in production for years. What are the risks?

IntermediateScenarioScenarios: writing and hardening scripts
Answer

Situation read → The script may rely on commands failing harmlessly: grep returning 1 when nothing matches, diff returning 1 when files differ, ((count++)) at zero, test as the last line of a function.

What I'd do →

  1. Grep for commands with meaningful non-zero codes: grep, diff, cmp, test/[, ((, read at EOF, pgrep, systemctl is-active.
  2. Make intended "failures" explicit: grep -q x f || true, if grep -q ...; then, count=$((count + 1)).
  3. Run it in staging with set -Eeuo pipefail and an ERR trap that logs the line, and fix each trip.
  4. Roll out behind a flag at first (STRICT=1) if the script is critical.

Why → set -e changes behaviour for every command; blindly adding it can turn a working script into one that stops halfway, which may be worse.

Interview tip → Saying "set -e is a safety net, not error handling" signals maturity.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: writing and hardening scripts