Magazine

Creating a Library of Functions in Linux Shell Script

Posted on the 29 April 2021 by Satish Kumar @satish_kumar86

If we want to create our ownlibraryof functions, then we can create a script and add all the functions into this script. We can make all the functions from our scriptfunctions.shavailable in the current shell by callingsourceor the period.command.

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

$ country USA

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

$ . functions.sh

Or it could display this one:

$ source functions.sh
$ country USA India Japan

This will execute thecountryfunction along with the parameter,USA India Japan.

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

my-library.sh

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

We have called the library function script my-library.sh inside another script. This will define all the functions within the script my-library.sh available in the current script environment.


Back to Featured Articles on Logo Paperblog