Cleanup in Bash Scripts

[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:

https://gist.github.com/lawrencegripper/9e778601b2a21d7891e46cf0e1765f46

Using Trap to fire cleanup on exit

Learn more here: https://www.linuxjournal.com/content/bash-trap-command