Creating a library of functions

If we want to create our own library of functions, then we can create a script and add all the functions into this script. We can make all the functions from our script functions.sh available in the current shell by calling source or the period . command.

The procedure to load all functions into the current shell is as follows:

$ countryUSA

Since the function country is not a part of the shell environment, this command will give an error:

$ . functions.sh

Or:

$ source functions.sh
$ country USA India Japan

This will execute the function country along with the parameter USAIndia Japan.

We can even load a script containing library functions inside another script as follows:

#!/bin/bash . /../my-library.sh call_library_functions(); ...

Get Learning Linux Shell Scripting 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.