Shell Script to Demonstrate the Use of Shell Function Library - GeeksforGeeks

11 May, 2021
Shell Function Library is basically a collection of functions that can be accessed from anywhere in the development environment. It actually makes shell scripting a bit less tedious and repetitive. By creating a shell script with some functions defined in it, we can then access and call those functions from other files or scripts.  It helps in avoiding repeating the code in large files and complex scripts.
Shell Function Library is kind of a header file in C/C++ or module in Python. The current script has to know that from where is the function defined. We also need to add the path of the file to the Environment variables to be able to access the functions or run the script in the terminal to load the function library into the current shell for temporary use.  Shell Script Functions are like normal shell scripts except they are only used to define the functions.
Creating function library:
#!/bin/bash
function square(){
    v1=$1
    n=$(($v1*$v1))
    echo $n
}

function expo(){
    v1=$1
    v2=$2
    n=$(($v1**$v2))
    echo $n
}

function factorial(){
    v1=$1
    n=1
    while [[ $v1 -gt 0 ]]; do
    n=$(($n*$v1))
    v1=$(($v1 - 1))
done
    echo $n
}
After writing and defining all the functions, we need to source the shell script and store it wherever we need to.
The Bash script will load the function from scanning everywhere, but that is the beauty of Shell Function Library, you need not worry about specifying the file path of the library of functions. It’s not necessary to include everything in a single library script. You can create various such files and call or use these functions from anywhere.
After that, we need a place or file where we can use or utilize this function library. So we create a shell script to call these functions and use it to avoid repetitive tasks and code.  
#!/bin/bash

echo "4^6 = "$(expo 4 6)
a=5
echo "$a! = "$(factorial $a)
b=18
echo "$b^2 = "$(square $b)
You can store the files accessible anywhere from your computer. The shell will look and search for the functions for you. You need to only use those functions from anywhere. But the script file that contains the functions should be sourced.
So from the example above it can be clear that the shell function library is quite powerful and useful. They can heavily decrease the code to be written. You can increase the complexity by accessing the function from one file to another and that can just be too powerful and quite a time-saving feature for some cases.
Shell Function Library is quite simple but it depends on the usability and the order of preference of the user as it can really mess up the code structure. The user might even forget where the actual function is stored if he needs to modify it. Shell Function Libraries are a great way to get organized without much of a hassle. It reduces the complexity of remembering or re-writing code again and again. It can save much time and effort with being able to tasks pretty easily and conveniently. This feature of the Linux shell can quite be remarkable in improving user’s productivity and time utilization in scripting files.
Picked
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Thread functions in C/C++
nohup Command in Linux with Examples
mv command in Linux with examples
scp command in Linux with Examples
Docker - COPY Instruction
chown command in Linux with Examples
nslookup command in Linux with Examples
SED command in Linux | Set 2
Named Pipe or FIFO with example C program
uniq Command in LINUX with examples