A Clean Approach to Formatting Shell Code
Beauty Contest
© Photo by Judy Beth Morris on Unsplash
When it comes to shell code, writing styles can vary. A few simple rules will help you catch errors and make it easier for others to view your work.
Although shell code admittedly has idiosyncratic syntax, it is not too difficult to learn. Due to its long history and diverse roots, various influences are reflected in the syntax. After all, the Bourne Shell has been around since 1979, and its successor, the Bourne Again Shell, since 1989. This is a blessing and a curse at the same time, as every programmer can leverage this diversity to suit their own taste and produce excellent code with just a few lines. The pitfalls lie in the inconsistency of notation, which makes it difficult to check for correctness (validation) and to subsequently format the code to ensure consistency.
ShellCheck [1] is a proven tool that will analyze shell code and look for problems. Another tool called shfmt [2] is a shell code formatter that helps you keep your code in an easily readable and verifiable form. A stable version of shfmt has been around for some time and is included in the package repositories of various distributions.
Formatting
No universal understanding currently exists for how to format shell code, nor is there a standard from an organization such as the International Organization for Standardization (ISO). Instead, it seems that programmers' habits and personal preferences play a major role. Not everyone agrees on what makes good shell code, which can be confusing for newcomers.
[...]
Buy this article as PDF
(incl. VAT)