Hey there,
It’s Robin from CFD Engine & I recently bumped into a couple of tools that have helped level-up my scripting (or at least make my scripts more robust).
Two open-source, cross-platform, command-line tools that will check your scripts for “bad practice”, explain the problems & show you how to fix them.
I’m a sucker for automation & shell scripts are my first port of call. If there’s an easy way to make them better, for free, then I’m listening.
If you’re not a scripting ninja 🥷 (& neither am I) then you might want to take a look.
Bash pitfalls
Bash scripts are the closest I get to developing code, but who knew there were so many ways to get things wrong 🤦♂️
And by “wrong” I mean “ways that might work now, but will probably cause trouble later.”
I’d quite like someone to save me from these bear traps & steer me in the right direction.
And that’s where the star of the today’s show comes in, enter shellcheck.
shellcheck
Shellcheck is several things, but as the name suggests, it’s a way to check bash scripts for common mistakes, misused commands, robustness & all sorts of other weirdness.
Here’s a non-exhaustive list of the kind of thing it checks 🤯
But the best bit is that rather than just flagging the stuff you did wrong, the issues come with an explanation, a suggested fix, or both (like this).
There’s a web interface where you can copy-paste your script, analyse it & apply the easier fixes without having to install anything. Great for a quick trial or for those who can’t install things locally.
It’s also available as a command-line tool for most platforms & as a plugin/linter for several code editors, including VSCode.
For context, it flagged 40 errors in my standard run script (mostly related to quoting variables) & that’s a script that I thought worked pretty well 🤭
Which brings us to the next tool…
shellharden
The command-line version of shellcheck
doesn’t offer to fix anything for you. That’s where shellharden comes in, another command-line tool, but with an option to automatically fix many of your scripting errors.
You still need to check what it’s doing (& that your script still works afterwards) but it’s a quick way to improve your scripts at a stroke.
My emerging workflow is to use shellharden
to correct the easy stuff, before running the hardened version through shellcheck
to pick up any other issues.
Ideally over time, I’ll stop making the same errors & my scripts will gradually improve 🤞
Check yo’ shell…
I recommend trying the online version of shellcheck even if you consider yourself a bash ninja.
Copy-paste one of your run scripts & see what it picks up, you might be surprised (I was).
Bonus link: Shellharden has a nice guide to doing things safely in bash, which is worth adding to your reading list (if you dig that kind of thing) 🤓
Before you go…
From next week the footer of these emails will look a bit different. I’m adding an invitation to book a coaching call if you’d like help figuring out how to apply these kind of things in your own workflows.
This is just a heads-up. I should have done it a long time ago, I hope you won’t mind it making an appearance now 🙏
The calls are paid, but the emails will always be free.
Until next week, stay safe,