Shell Script Debugger
Debug and fix shell scripts with systematic troubleshooting techniques. Identifies common error patterns, applies debugging tools, and teaches robust scripting practices that prevent bugs.
Usage
Paste your shell script or describe the error you're encountering. The debugger identifies the issue, explains why it happens, and provides the fix along with prevention techniques.
Parameters
- Shell: Bash, Zsh, POSIX sh, or Fish
- Issue: Syntax error, Unexpected behavior, Performance, or Hardening review
- Environment: Linux, macOS, Docker, or CI/CD pipeline
- Script: Paste the problematic script
Examples
- Variable Expansion Bug: Debug a script failing on filenames with spaces — explain quoting rules, demonstrate
"$var"vs.$var, and fix withset -ufor unbound variable detection.
- Pipeline Error Handling: Script succeeds despite a failed command in a pipeline — explain
set -o pipefail, demonstratePIPESTATUS, and implement proper error trapping with cleanup.
- Heredoc Indentation: Fix heredoc issues with tabs vs. spaces —
<<-EOFfor tab-indented heredocs, variable expansion control with<<'EOF', and common quoting mistakes.
- Performance Optimization: Speed up a script processing 10,000 files — replace subprocess-per-line with
while readloops, usefind -exec +instead ofxargs, and avoid unnecessary pipes.
Guidelines
set -euo pipefailis recommended as the first line of every production scriptset -x(orbash -x script.sh) traces execution for step-by-step debugging- Common errors are categorized: quoting, word splitting, globbing, exit codes, subshells
- ShellCheck (static analysis) is recommended for catching issues before runtime
- Variable quoting rules are explained with specific examples of what breaks without quotes
- Array handling differences between bash and POSIX sh are addressed
- Process substitution, command substitution, and subshell scope are clarified
- Trap handlers ensure cleanup runs on EXIT, ERR, and signal interrupts
- Portable vs. bash-specific features are distinguished for cross-platform scripts
- Script templates provide a safe starting point with error handling built in