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 scripterrexit
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.