Defer for Shell (2020)

The author of this article highlights how the defer keyword in Go allows for a simple way to clean up resources upon function return, reducing the likelihood of bugs. They believe it’s so useful that it should be part of every language, especially for freeing locally allocated memory in C. The author has even implemented a pseudo-defer keyword for shell scripts which ensures that mounts and files are always cleaned up properly, even if the script exits early. They provide an example of how the function can be used for mounting and unmounting files or creating and deleting temporary files.

https://cedwards.xyz/defer-for-shell/

To top