Last week I published an npm package called create-phone-number-forwarding. It wraps a shell script that uses the Twilio CLI. If you run the script, it buys a Twilio phone number and sets everything up to forward incoming SMS/calls to another number.
While doing that I learned to things:
- shell scripting is tough
- one should always use
setin custom shell scripts
The task of writing this script took me way longer than expected. I was going back and forth because the error handling in bash is not great – or at least that was what I thought.
It turned out that shell scripts should include one line before kicking things off.
set -euo pipefail
This one line lets the script fail after any non-zero command execution (
-e), it'll throw if you use undefined variables (
-u) and will help you not to miss errors and non-zero status codes in pipes (
In short: this one line makes your shell scripts more robust because it will fail more often! 🎉
If you want to read more on
set and its flags, Aaron Maxwell wrote a nice tutorial about the subject.
PS. I also came across ShellCheck, which you might want to give a try when writing shell scripts. :)