🐛

Shell Script Debugger

Verified

by Community

Diagnoses shell script issues with debugging techniques, common error patterns, best practices for robust scripting, and tools for tracing execution flow.

systemshelldebuggingbashscriptingtroubleshooting

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

  1. Variable Expansion Bug: Debug a script failing on filenames with spaces — explain quoting rules, demonstrate "$var" vs. $var, and fix with set -u for unbound variable detection.
  1. Pipeline Error Handling: Script succeeds despite a failed command in a pipeline — explain set -o pipefail, demonstrate PIPESTATUS, and implement proper error trapping with cleanup.
  1. Heredoc Indentation: Fix heredoc issues with tabs vs. spaces — <<-EOF for tab-indented heredocs, variable expansion control with <<'EOF', and common quoting mistakes.
  1. Performance Optimization: Speed up a script processing 10,000 files — replace subprocess-per-line with while read loops, use find -exec + instead of xargs, and avoid unnecessary pipes.

Guidelines

  • set -euo pipefail is recommended as the first line of every production script
  • set -x (or bash -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