How To Improve Shell Script Code Quality and Readability

Introduction

This guide will show you how to configure the Atom editor to improve shell script code quality and readability using the linter-shellcheck and format-shell packages in the Atom editor on a Windows 10 device.

Atom

Atom is a free and open source code editor for macOS, Linux and Microsoft Windows with support for plug-ins developed by GitHub. You can choose from thousands of open source packages that add new features and functionality to Atom. Atom also comes preinstalled with four UI and eight syntax themes in both dark and light colors. It is easy to customize and style Atom to fit your needs. Atom is a desktop application built with HTML, JavaScript, CSS and Node.js integration.    

Download the Atom editor from the official site at https://atom.io/

shellcheck Installation

For shellcheck to work, you must first download the appropriate shellcheck precompiled binary file for your operating system.

Install the package “linter-shellcheck” in the Atom editor

  • From the Atom menu, select Packages -> Settings View -> Install Packages/Themes
  • In the “Search packages” bar, enter linter-shellcheck
  • Once the package appears in the list, select the Install button
  • Once installation is complete, select the Settings button
linter-shellcheck Settings Screen

Below the Settings section, enter the path and file name of the precompiled windows binary file you downloaded from https://shellcheck.storage.googleapis.com/index.html in the Shellcheck Executable Path field.

Shellcheck Error Messages

When you open a shell script in the Atom editor, a warning symbol appears to the left of the line number for any code that has warnings or errors. Hovering on the warning symbol will display a message with more information about the issue along with an “SC” number. To get additional information about the warning message, go to the website https://github.com/koalaman/shellcheck/wiki/SC2039 and append the “SC” error number to the URL as done in the provided example. The website will provide additional information about the problematic code along with an example of how to correct the code.

Format-Shell Package Installation

The Atom Format Shell package will automatically format, beautify and pretty-print your shell script source code from within the Atom text editor.

The formatting is powered under the hood by shfmt, which must be installed separately from Atom and the Format Shell package.

Download the shfmt executable

  • Go to https://github.com/mvdan/sh/releases to download the shfmt executable for your operating system
  • If you have an 64-bit Operating System, x64 based-processor, download the file shfmt_v2.6i.4_windows_amd64.exe
  • Save the file to a location that you can easily reference

Install the Atom package “format-shell”

  • From the Atom menu, select Packages -> Settings View-> Install Packages/Themes
  • In the “Search packages” bar, enter format-shell
  • Once the package appears in the list, select the Install button
  • Once the package installation is complete, select the Settings button
format-shell Package Setting Screen

Below the Settings section, enter the path and file name of the shfmt file you downloaded in the “Path to shfmt executable” field.

Format-Shell Usage

To format a shell script, open up the shell script in the Atom editor. Then, select Packages -> Format Shell Script from the menu.  The script will then “beautify” the code by removing DOS line endings and formatting for readability.

To format a shell script, open up the shell script in the Atom editor. Then, select Packages -> Format Shell Script from the menu.  The script will then “beautify” the code by removing DOS line endings and formatting for readability.

Enable Autosave

While in Atom, enable the Autosave package. The package will automatically save the editor if it looses focus, is destroyed, or when the window is closed.

  • From the menu, select Packages -> Settings View -> Manage Packages
  • In the “Search packages” bar, enter autosave
  • Select the Settings button
autosave Package Settings Screen

In the Settings screen, scroll down to the Settings section and check the Enabled box.

Conclusion

The Atom editor is a very rich and powerful editor containing thousands of open source packages that adds new features and functionality to Atom. The Atom packages linter-shellcheck and format-shell will help you improve shell script code quality and readability during the development process. Because you are developing the code on your local machine, I recommend that you install an SFTP client, such as WinSCP, on your Windows machine to easily transfer the code to the Linux operating system for testing.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top