Preamble
Bash scripts tend to be ad-hoc and written for a specific purpose at a particular time on some given input. The scripts just need to work, no matter how arcane the syntax or how obscure the code is. As these scripts accumulate, it becomes difficult to modify them and make them work together. While Bash is certainly not intended for writing highly structured and complex programs, it is still worthwhile to write clear, modularized, well-documented Bash code, because pieces of code are read much more often than they are written.
Accordingly, this guide is not a cookbook, or a collection of recipes or examples, from which snippets can be copied-and-pasted (the Internet abounds with such resources); rather, this guide will introduce you to Bash programming using modern language features that reduce human errors and unexpected behaviours. This goal, unfortunately, clashes with the competing desire to write portable code that works across shells. Bash is one of several shell programming languages and extends the set of POSIX Shell Command Language standards. Frankly, we are not interested in writing shell scripts; we are interested in writing Bash scripts. Bash is open source and runs across most platforms. Despite that we will be using Bash-specific features, our scripts will run on all platforms on which Bash can be installed. When multiple ways to code a particular task exists, we will choose the clearest and simplest, even if they use Bash-specific features. We will, however, attempt to write Linux platform independent code as much as feasible. To emphasize, the foremost learning objective of this guide is to write robust code that is easy to troubleshoot when unanticipated problems arise.
Bash is often purported to be an easy programming language to learn, but has been a source of frustration for me when I first started using it. Hopefully, this guide will help you embark on the right course.
Last updated