Published at
Updated at
Reading time
1min
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. :)

If you enjoyed this article...

Join 5.5k readers and learn something new every week with Web Weekly.

Web Weekly โ€” Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
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