Safety for Shell and Bashs
Script safety means writing Bash that fails loudly and predictably instead of continuing after an error. It covers exit codes, set -e, set -u and pipefail, trap handlers for cleanup, and tools like bash -x and ShellCheck for finding bugs. It sits in the robust-scripts stage because unsafe scripts can delete or overwrite real data.
Stage: Robust Scripts · Part of the Shell and Bash roadmap · Last reviewed 19 November 2026
What to learn in Safety
- Check exit codes with $? and use && and || to branch on success
- Enable set -euo pipefail and explain what each option changes
- Use trap to clean up temporary files when a script exits
- Trace execution with bash -x and print debug output to stderr
- Run ShellCheck and fix its warnings about quoting and portability
Safety interview questions
What does set -euo pipefail do?
The -e option exits on a failing command, -u treats unset variables as errors, and pipefail makes a pipeline fail if any stage fails, not just the last. Together they catch many silent failures, though -e has edge cases inside conditions and subshells.
Why can rm -rf "$dir/" be dangerous in a script?
If dir is unset or empty, the path becomes /, and the command can try to remove from the filesystem root. Using set -u, checking the variable with ${dir:?}, and quoting it avoid this class of disaster.
Where Safety fits in the Shell and Bash roadmap
Safety belongs to the Robust Scripts stage (beginner level), which takes about 9 hours. Reliability.
Other units in this stage
Back to the full Shell and Bash roadmap