[Brain dump so I don’t forget this one]
So you want your bash script to exit on an error but you’d like it to clean some stuff up before it closes after the error occurs.
No problem a TRAP
can do this for you (read detailed docs for caveats).
In a very simple form it looks like this:
Learn more here: https://www.linuxjournal.com/content/bash-trap-command