Published at
Updated at
Reading time
This post is part of my Today I learned series in which I share all my web development learnings.

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 set in 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 (-o pipefail).

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. :)

Was this TIL post helpful?
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 4 days ago.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles