Chris's Wiki :: blog/programming/ShellScriptsBeClearFirst

submited by
Style Pass
2022-05-27 04:00:08

When writing shell scripts, there's often a general tendency towards minimalism and what could be called 'code golfing'. After all, one of the important things for shell script performance is to run as few extra commands as possible. However, shell scripts have another problem, which is that for various reasons the shell is not a great programming language (see parts of my entry on why large shell scripts have problems). In particular, shell scripts often lack clarity because they have to do a lot of things indirectly.

In the past I've said that the normal problem with configuration systems is that they lack clarity more than they lack power. Similarly, shell scripts more often lack clarity than they lack performance, so I'm strongly of the feeling that you should be biased toward clarity in your shell scripting. One aspect of this is being careful of where you're being clever. Every clever, efficient idiom chosen over the obvious but slightly less efficient version is a potential future tripping hazard (for other people and your future self).

Some of what you consider (too) clever will depend on the degree of knowledge of shell scripting you want to assume. For example, there are a lot of options for manipulating variable expansion to do useful things that save you from running external programs. Should you use them? Maybe. I'm no longer so sure of that for here, because I'm just not that immersed in shell scripting (and neither are my co-workers). But arithmetic is pleasant and obvious enough that I'd definitely keep it rather than resorting to other options.

Leave a Comment
Related Posts