Creating a library of functions in Linux shell script

April 29, 2021

Comments

Adsense

Adsense

Adsense

Adsense

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:

$ 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 the country function 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.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Related Posts

Backup of files in Linux using command line

In IT or our day-to-day computer industry activities, taking backup is one of the most important activities. Previously, offices were required to keep important paper in a safe place; but if a fire breaks out, then everything is finished. In the digital world, taking...

Using awk in Linux

awk is a program that has its own programming language for performing data-processing and generating reports. The GNU version of awk is gawk. awk processes data, which can be received from a standard input, input file, or as the output of any other command or process....

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

5 × 2 =

News & Updates

Join Our Newsletter

Adsense

Adsense

Adsense

Adsense