a c d w

how I write shell scripts

I write a lot of shell scripts. Like, a lot a lot. Honestly, I quite shell scripts, though I hear from nearly everyone that they're hard to write and read and they're full of footguns.

This is only partially true.

Like, yeah, shell scripts are fiddly and you can easily make an error in them that is pretty hard to make in a "real" language. And I definitely wish arrays were possible (in POSIX shell) or less-terrible to use (in Bash).

However, with a few fairly easy-to-remember best practices, I find shell scripting only mildly less convenient to write than other types of scripts. Plus, since I already know the shell's "standard library" (coreutils and the other programs I have installed), it has the edge for many small-to-medium-sized programs that I write.

This is not meant to be an exhaustive list of How To Write Good Shell Scripts. For a resource in that direction, I recommend the following excellent resources:

structuring shell programs

a functional style with just a bit of global state

variables

bonus: writing a Makefile for your shell script