Bash Debugging

Debugging and best practices

Debugging Options

bash -x script.sh # execute with debug trace
set -x # enable debug mode
set +x # disable debug mode
set -e # exit on error
set -u # exit on undefined variable

Error Handling

set -euo pipefail # strict mode
trap command signal # catch signals
trap cleanup EXIT # run cleanup on exit

Best Practices

# Always quote variables
echo "$variable"

# Use [[ ]] instead of [ ]
[[ $var = value ]]

# Check command exists
command -v cmd >/dev/null

Script Template

#!/bin/bash
set -euo pipefail

# Your code here