How to debug a Bash script?

How to debug a Bash script if it has some bugs?

Common techniques like printing varibles out for checking apply for bash too.

For bash, I usually use 2 bash-specific techniques.

Add set -o errexit to the beginning of the script
errexit makes bash exit immediately if one statement’s return code is not 0. This way, you know which statement goes wrong.

bash -x your-script.sh
the -x will make bash print statements executed so that you know what is going on.

By combining the 2 techniques together, which statement/step went wrong is printed out on the STDOUT.

Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *