Only Use Bash When

Published: 2021-12-31
Tagged: linux programming productivity

It's the last day of 2021 so I'll keep this short and sweet: Only use bash when your problem is about handling lines of text?

Why?

Programming languages are tools for thinking. Each one provides you with different abstractions, forcing your thoughts down different paths.

If you apply functional programming to a problem, you will think in lists and trees and recursion and whatnot. But switch to object-oriented programming and now you're thinking in classes, relationships, and message-passing.

With bash, your main abstraction is lines of text. And if that's what your problem is about then bash is great. It will handle file IO, argument parsing, and even composability (w/ pipes). Just knowing grep, sed, and awk will get you through 95% of problems.

But if you find yourself working with JSON or YAML or any other actual data structure, bash will make you miserable. You'll have problems with newlines and with collections and you will spend precious time muddling through, helping yourself with 10 year old StackOverflow answers about parsing HTML with regex.

Sure, eventually you'll arrive at a solution. And maybe you'll end up using something like jq to help with the JSON bits. But consider this: whoever has to maintain this solution will either have to unpack complicated bash or learn a new tool like jq--all just to do something stupid simple like "check if item is in a collection."

So here's a helpful rule of thumb: if your problem involves handling lines of text, use bash; but if it's anything more complex, do yourself a favor and go straight for your preferred programming language.

You may still be tempted. Without bash, you'll have to open and close files and import libraries and parse arguments. All boring overhead.

But for this price, you get to work close to your problem domain: treat lists like lists, maps like maps, trees like trees, etc. In other words, you'll avoid trying to awkwardly force the problem into bash's "everything is text" paradigm.

Happy New Year.

Comments

There aren't any comments here.

Add new comment