Shell script best practices, from a decade of scripting things
Hands down, shell scripting is one of my all time favorite languages. It gets tons of hate, e.g. &quot;If you have to write more than 10 lines, then use a <i>real</i> language,&quot; but I feel like those assertions are more socially-founded opinions than technically-backed arguments.<p>My basic thesis is that Shell as a programming language---with it&#x27;s dynamic scope, focus on line-oriented text, and pipelines---is simply a different programming paradigm than languages like Perl, Python, whatever.<p>Obviously, if your mental model is BASIC and you try to write Python, then you encounter lots of friction and it&#x27;s easy for the latter to feel hacky, bad and ugly. To enjoy and program Python well, it&#x27;s probably best to shift your mental model. The same goes for Shell.<p>What is the Shell paradigm? I would argue that it&#x27;s line-oriented pipelines. There is a ton to unpack in that, but a huge example where I see friction is overuse of variables in scripts. Trying to stuff data inside variables, with shell&#x27;s paucity of data types is a recipe for irritation. However, if you instead organize all your data in a format that&#x27;s sympathetic to line-oriented processing on stdin-stdout, then shell will work with you instead of against.<p>&#x2F;2cents