The Ultimate Guide to Modularizing Bash Script Code

It’s easier than you think to organize your Bash script

Shinichi Okada
mkdir Awesome

--

Image by Text To Photo Editor. Background photo by Vanessa Bucceri on Unsplash.

[Update: 2021–12–29 Added utils.sh]

Table of Contents
· Introduction
· Structure
· Option parser, getoptions
· Defining script root directory
· Note for Debian/Ubuntu
· Set definition variables
· Importing(sourcing) files
Sub-command parser definition
Importing JavaScript in a sub-command file
· Main parser definition
· Environment check and other functions
OS check
Dependency check
· Argument parsing
· Conclusion

Introduction

I have created quite a few Bash script projects for macOS/Homebrew and Debian/APT. It often makes things easier to modularize Bash script code when a project gets big. With your modularized code, you don't need to scroll up/down your file to find what you are looking for to make your code simpler. In this article, you will find the best practices on how to modularize Bash script code using my Teffects project.

Teffects is a Bash script command line app and it creates different header images. It has more than one hundred files.

[Update] I have implemented all tips in this article to a Shell Script Starter.

--

--

Shinichi Okada
mkdir Awesome

A programmer and technology enthusiast with a passion for sharing my knowledge and experience. https://codewithshin.com