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 www.shellcheck.net (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 https://sipb.mit.edu/doc/safe-shell/
Update: how to write
idempotent Shell Scripts:
I Recently found this Blog Post, and I think it is a great addition to this topic!
Beware though that some of the Actions described in the Blog Post are NOT truly idempotent, as i.E.
touch will alter the modification time of the file, and forcefully creating/recreating/deleting things are changes! But the Ideas and Concepts conveyed in this Article are good non the less, and it is an intersiting Read!
You still should use a Tool like Ansible or another truly idempotent Configuration Management Solution in most cases anyway tough!