Even Google can't write shell scripts

Google’s Shell Style Guide — via Hacker News — doesn’t mention using set -e or set -u, and neither does Gentoo’s guide, which seriously undermines them both. They both have sensible advice, but those two settings are the two most important things to include when scripting with Bash. Bash doesn’t even become a scripting language until those are set; without, scripts are just interactive transcripts without the interactive part, i.e. a human to stop execution when there are errors or unexpected behaviour.

My advice, before you read either of these guides, is to put the following at the top of every script you write, before you start: set -eu. When working with a team put in the long version, with comments:

# Exit immediately if a command exits with a non-zero status.
set -o errexit
# Treat unset variables as an error when substituting.
set -o nounset

If you know me, you’ll know that that’s not really my advice. My really real advice is: don’t write scripts with Bash, or most other shells. Learn and use Python instead, for example. Sure, shell scripts are convenient, but you really need 5+ years of writing shell scripts to understand enough of the many insidious ways that they’ll break on you. By which time I truly hope you will have learned that you shouldn’t use them except as short-lived conveniences. Even then I’m not sure.

Fwiw, neither Google nor Gentoo mention testing either.