#12 Building a Shell Script Library

Many of the scripts in this chapter have been written as functions rather than as stand-alone scripts so that they can be easily and gracefully incorporated into other scripts without incurring the overhead of making system calls. While there's no #include feature in a shell script, as there is in C, there is a tremendously important capability called sourcing a file that serves the same purpose.

To see why this is important, let's consider the alternative. If you invoke a shell script within a shell, by default that script is run within its own subshell. You can immediately prove this experimentally:

$ cat tinyscript.sh
test=2
$ test=1
$ tinyscript.sh
$ echo $test
1

Because this script changed the value ...

Get Wicked Cool Shell Scripts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.