Use Bash Strict Mode (Unless You Love Debugging)
Let's start with the punchline. Your bash scripts will be more robust, reliable and maintainable if you start them like this:
#!/bin/bash set -euo pipefail IFS=$'\n\t'