Tips and Resources for writing better Shell Scripts

It is easy to write bad shell scripts, and there is a lot of room for mistakes!, possibly the best tip one could give is to use a higher-level language, like python, and try to avoid writing shell scripts at all. But if you just have to, or want to, here are some helpful Tips/Resources you can use to write better shell scripts ;)

Use the Google Shell Style Guide

Using a consistent coding style among your Team/Company is important, the same applys to shell scripts as well! Try to find out if there already exists a guideline within your Structure, and adopt it.
If there is none in place, you should take a look at Googles Shell Style Guide which is a really great reference! (Maybe also check out their other Style Guides)

Use ShellCheck for linting

While using Linters with "real" programming languages, like python, is the default and most specialized IDE's have linting functionality neatly integrated, most people don’t think about linting their bash scripts, as they are just considered to be "quick hacks or whatever".

Nevertheless it comes as a surprise that there also exist linters for bash and ShellCheck is a really good one!
You can easily install it locally and/or try it on-line by visiting (dont blindly copy and paste your secrets into that, or any other website ;) )

Most modern Text editors like Sublime or Atom also have a great ShellCheck integration, give it a try!

Use Shell settings to increase script safety

If a command within your shell script goes wrong, a variable is not set or a piped command errors, the default behavior of bash is to, more or less, silently fail and go on with the script, as if nothing happened. This can be rather dangerous, and often results in unintended script behavior.

To increase script safety and avoid unintended behavior, you must explicitly tell your shell to automatically exit when an error occurs!

This can be done by setting set -eu -o pipefail at the beginning of your script you tell bash to stop and exit immediately when a Step fails!
Read more about writing safe shell scripts, over here

references and further reading material