In Zsh, the power is immediate. That power can also be dangerous. Without guardrails, one slip can wipe files, destroy configs, or push broken code to production. Dangerous action prevention in Zsh is not optional—it’s survival.
The way forward starts with awareness. Zsh offers native hooks, aliases, and pre-exec functions to intercept risky commands before they run. These features can detect patterns like rm -rf /, overwriting files without backup, or force-pushing to critical branches. They stop the command cold and prompt you to think before confirming.
To set this up, you can use preexec_functions to inspect your input for unsafe terms. You can design rules that abort execution for destructive commands unless a deliberate override flag is set. Pair this with noclobber to prevent accidental file overwrites, and set -o noclobber stays as a quiet, continuous safeguard.
History settings matter too. A well-tuned HIST_IGNORE_SPACE can hide sensitive commands from history when prefixed with a space. HIST_VERIFY forces a preview before execution when pulling from history. These seem small, but they eliminate common muscle-memory mistakes.