To debug a bash script you can always write:

bash -x <scriptname>

this will write all commands and output of all commands to the terminal. You can see further information with the good old man pages man bash or examples with tldr bash.