Step 1 to write safer shell scripts: use a safer shell. Zsh gives the user much more control, has safer defaults, and is itself quite portable (even if Zsh scripts are not portable to other shells).
unfortunately, zsh is installed on probably about 1% of Linux systems worldwide. maybe it can be installed almost anywhere, but the fact is it isn't, and you might as well use Python or something at that point.
And how many on the system that you actually touch? How hard is it to (ask to) install a new shell?
Obviously if you're distributing your script publicly no one would write that in zsh compatible way.