Enable Debug For Shell Scripts

Debugging shell scripts comes handy, especially when unexpected things happen. Trust me, they do. I use two different approaches, based on what’s needed: debug while running it or when ran in the background, maybe by cron.

  1. Instead of ./script.sh run sh -x script.sh or bash -x script.sh
  2. When the script is invoked by cron things are slightly different, though still simple:
    • add set -x to your script, after #!/bin/sh
    • keep an eye on /var/log/messages & /var/log/syslog

Enjoy 😉

PS: The bash reference manual has many useful information

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.