Using the “exit trap” pattern in Bash scripts can make them more reliable. By trapping the pseudo-signal “EXIT”, code placed in a “finish” function can ensure necessary cleanup operations are always performed, even in the event of an unexpected error. Without this pattern, there is a risk of resource leaks and other maintainability problems. The exit trap can be useful in multiple scenarios, such as ensuring a server restarts after temporary maintenance or creating custom Amazon Machine Images. Using this pattern can help make Bash scripts more robust and reliable.
http://redsymbol.net/articles/bash-exit-traps/