Debugging & Safety in Bash

From the Bash Scripting cheat sheet · Debugging & Best Practices · verified Jul 2026

Debugging & Safety

Tracing, linting, and guidelines.

bash
# Quick Reference
bash -x script.sh        # trace every command
set -x                   # trace on
set +x                   # trace off
shellcheck script.sh     # static analysis
💡 Run scripts through ShellCheck - it flags quoting, unset vars, and portability bugs.
⚡ bash -x (or set -x) traces each command with its expanded values - the fastest debugger.
📌 Quote every expansion, use [[ ]] over [ ], and $(...) over backticks by default.
🟢 : "${1:?usage...}" is a one-line guard that aborts when a required argument is missing.
debuggingbest-practices
Back to the full Bash Scripting cheat sheet