A few weeks ago, I chanced upon a blog post by Aaron Maxwell of redsymbol.net, titled Use the Unofficial Bash Strict Mode (Unless you Looove Debugging). I thought it was brilliant, and just want to share some of my thoughts after having used this unofficial bash strict mode since that time.
If you haven’t read Aaron’s blog post, you should.
If you haven’t started using his unofficial bash strict mode, you should, too.
The evolution of my original unofficial bash strict mode
For a very long time before I read Aaron’s blog post, this was my unofficial bash strict mode, which I used in most of my bash scripts:
It is certainly much less comprehensive than Aaron’s unofficial bash strict mode, having the effect of erroring out on commands which return a non-zero exit code. But this flag alone eased a lot of debugging pain for bash scripts.
After some time, I encountered
set -u, which errors out on the use of undefined variables. So my unofficial bash strict mode became:
which is just 2 things shy of Aaron’s unofficial bash strict mode. With
set -u, debugging bash scripts becomes even easier, since use of undefined variables is a big source of bugs, especially when we factor in typos.
Thoughts on the other 2 flags
In fact, with
set -eu, writing bash scripts becomes very smooth for me. So as much as Aaron’s blog post convinced me that his unofficial bash strict mode was superior to the one I’ve been using, after adopting its usage, I didn’t really detect much noticeable difference in how
set -o pipefail and
IFS=$'\n\t' helped with debugging, or how I had to adjust my code to work around some of their quirks.
That being said, if I were to choose among those 2 to aid in the ease of writing bash scripts, it’ll be:
set -o pipefail
Since that is such a natural next step from
set -eu. And detecting intermediate failures in a very long chain of commands in a pipeline is an extremely welcome addition.
IFS=$'\n\t', while it does seem useful, I’m still a bit confused about bash arrays and don’t use them too often in my bash scripts. For string manipulation, I rely on a mix of
perl and a little bit of
sed and when I’m really really confused about those 3 tools, I stick to a trusty Python script. So it is probably the least useful addition to my arsenal. Hopefully I’ll be making full use of it as my bash-fu improves. But the thing about bash is, it is actually quite difficult to pick up, or at least it gives people the impression of that. There is probably some truth to both, seeing how quite a number of programmers I know don’t know how to write bash scripts and see it as some kind of alien language when I get them to fire up their text editor to open a bash script I’ve written so we can debug something in it together; and for those who know how to write them, it certainly isn’t their favourite language and they’d rather avoid it if possible.
Wishing all readers a Merry Christmas! If you didn’t know about Aaron’s unofficial bash strict mode:
set -euo pipefail IFS=$'\n\t'
I hope this is a good Christmas gift for you. Happy Holidays!