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)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Working Exploit for a Linux Kernel Flaw Made Public
Even before a bug received its CVE identifier or an upstream fix, the local privilege escalation vulnerability exploit found its way into the wild.
-
r/opensource Bans AI-Assisted and AI-Related Projects
The de facto subreddit for all things open source has not only banned AI-related projects but also users who "don't speak English."
-
Bottlenecks Discovered by Meta Engineers Lead to Scalability Improvements
Thanks to a small group of engineers at Meta, the Linux kernel module kernfs has been improved, which has led to some serious gains.
-
PwnEscape Poses VM Vulnerability
A guest-to-host escape flaw in Linux KVM, named PwnEscape, might be yet another means of attacking Linux.
-
Linux Mint 27 Set for a Christmas Release
The developers behind Linux Mint have announced a Christmas release, and it looks like Santa's sack will be brimming with features.
-
Is Linus Torvalds Vibe Coding?
The creator of Linux has added AI into his process for Linux kernel development, and it's more than just for debugging.
-
openKylin 3.0 Released with Expanded AI Integration
The latest release features a pre-installed AI agent to help perform tasks.
-
DigitalOcean Ends Open Source Credits Program
In a change that has surprised many open source developers, DigitalOcean has decided to sunset its Open Source Credits Program.
-
Office 365 Available for Testing on Linux via Bottles
Linux users can now test the official Office 365, thanks to Mirko Brombin's work on Bottles.
-
Dutch Government Turns to Open Source for Digital Sovereignty
Following in the footsteps of other European countries, the Netherlands is looking to increase its sovereign digital infrastructure with open source software.
