Bash – Is it common to split larger script into multiple scripts and source them in the main script

bash

At the moment I'm developing a larger Bash script (it's an Open Source project of mine) and it's starting to become a mess. I've split up logic into functions, use local variables where I can and have only declared a handful global variables. Still, it is becoming pretty hard to maintain.

I thought about splitting the script up into multiple scripts and source them in my main script (similar to imports in other languages).

But I wonder if this is a feasible approach. First, sourcing multiple scripts could severly slow the execution time of the script, and second, it makes distribution harder.

So, is this a good approach, and do other (Open Source) projects do it the same way?

Best Answer

Yes, it is a common practice. For example, in the early days of Unix the shell code responsible for guiding the system through its boot phases to multiuser operation was a single file, /etc/rc. Today the boot process is controlled by many shell scripts, broken up by function, with common functions and variables sourced as needed from central locations. Linux distributions, Macs, BSDs, all have adopted this approach to varying degree.

Related Question