Skip to main content

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

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.

Back to the full Shell and Bash roadmap