Shell Script to Demonstrate the Use of Shell Function Library
Last Updated :
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.
Using Functions From Library:
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.
Usage of Shell Function Library
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.
Similar Reads
Introduction to Linux Shell and Shell Scripting If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
Shell Script to Demonstrate Special Parameters With Example Here, we are going to see what are the special Parameters of the shell script. Before that first, let's understand what is parameters in the shell. The parameter is the entity that stores the value. The variables are the parameters that are defined by the user to use in that specific shell script. A
5 min read
Shell Scripting - Functions and it's types Shell scripting is a powerful tool used to automate tasks in Unix-like operating systems. A shell serves as a command-line interpreter, and shell scripts often perform file manipulation, program execution, and text output. Here, we'll look into functions in shell scripting, exploring their structure
5 min read
Shell Scripting - Interactive and Non-Interactive Shell A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell
3 min read
Difference between Thompson Shell and POSIX Shell The Thompson Shell and the POSIX Shell are Unix command-line interpreters, however, they are different in their uses and were created at different time of Unix advancement. The Thompson Shell is one of the first Unix shells developed in the first half of the 1970 s and provided basic functionalities
5 min read