This one took me forever to figure out, so documenting it here in the hopes I’ll remember it now. ;]
If you have a function that is sourced from a bash script, it’s hard to debug because you can’t just do the usual {bash}bash -x script.sh{/bash} thing – all that will do is run the sourcing of the file through the debugger, but unless you actually call your function from the file, it’s not going to run.
bashdb
to the rescue! With the bash debugger you can do the following:
bashdb --init-file script.bash -c "function_to_call param1 param2"
Hooray! Now bashdb will source the file for you and call your function, which you can then step through to figure out where you messed up. Awesome.
Probably plenty clear to lots of people out there, but I didn’t know it. HTH.
bashdb
needs to be installed on MacOS – you can usebrew install bashdb
for that.https://unix.stackexchange.com/questions/521775/how-to-debug-trace-bash-function also illustrates some other examples, so e.g. you could do
set -xT; function_to_trace -w ith -p arams; set +xT
.