How to quickly fail bash scripts using set (#tilPost)

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 …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2020-02-25T23:00:00+00:00) How to quickly fail bash scripts using set (#tilPost). Retrieved from https://www.scien.cx/2020/02/25/how-to-quickly-fail-bash-scripts-using-set-tilpost/

MLA
" » How to quickly fail bash scripts using set (#tilPost)." Stefan Judis | Sciencx - Tuesday February 25, 2020, https://www.scien.cx/2020/02/25/how-to-quickly-fail-bash-scripts-using-set-tilpost/
HARVARD
Stefan Judis | Sciencx Tuesday February 25, 2020 » How to quickly fail bash scripts using set (#tilPost)., viewed ,<https://www.scien.cx/2020/02/25/how-to-quickly-fail-bash-scripts-using-set-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to quickly fail bash scripts using set (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/02/25/how-to-quickly-fail-bash-scripts-using-set-tilpost/
CHICAGO
" » How to quickly fail bash scripts using set (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2020/02/25/how-to-quickly-fail-bash-scripts-using-set-tilpost/
IEEE
" » How to quickly fail bash scripts using set (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2020/02/25/how-to-quickly-fail-bash-scripts-using-set-tilpost/. [Accessed: ]
rf:citation
» How to quickly fail bash scripts using set (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2020/02/25/how-to-quickly-fail-bash-scripts-using-set-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.