How to debug a Bash script?

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

asked Jun 5 by anonymous

1 Answer

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 Sep 2 by Eric Z Ma (43,420 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...