Pcornel
Automation · Bash Automation

Bash Troubleshooting

Cheat sheet · https://pcornel.dk/topic.php?topic=5291

Back to the Topic

Answer

Troubleshoot Bash scripts with bash -x or set -x to trace commands, shellcheck for common mistakes, checking unquoted variables, Windows line endings (CRLF) and the environment when run from cron.

Remember

  • bash -x shows each command.
  • Unquoted variables split on spaces.
  • cron has a minimal environment.

First checks

  • bash -x script.sh — trace
  • shellcheck script.sh — common bugs
  • env -i PATH=/usr/bin:/bin bash script.sh — reproduce cron env

Commands

bash -n <script.sh>
Read-only/validation observation for Bash parser/syntax result without executing the script body.

Verify in this order

failure → trace → cause → fix

Where it usually breaks

Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5291

Confirmation

Confirm action

Continue?