Many bash scripts start out as something quick and dirty but then become so useful that they live for years, indeed sometimes seeing more use than our traditional programs. Now that you can even run bash well under Windows (although, you’ve always been able to run it there if you tried), there are even more opportunities for your five-minute bash script to proliferate. [Maciej] decided he was tired of always having to patch up his quick and dirty scripts to be more robust, so he created (and shared) his boilerplate template for scripts.
Probably most of us have at least some basic template we start with, even if it just our last script project. What’s nice about [Maciej’s] template is that he documents what’s going on with each part of it. It is also relatively short without a lot of excess stuff. Of course, you’ll probably customize it, but it is a great place to start.
The template has a good shell selection line using env and also sets up basic error handling. That includes a trap function that runs on termination and argument parsing including a help message. There’s a oneliner that sets the current directory to the script directory (although you don’t always want that).
The only real excesses in the template are the short example script and a function that sets colors based on the terminal type that you might not need for your script. However, it is nice to print warnings with highlight color when they are not redirected and this code handles that for you.
If you don’t want to bother yanking the code from a web page, the template is also on a GitHub Gist. As [Maciej] points out, there are other similar projects out there, but some of them are a bit bloated.