Updated April 10, 2023
Introduction to Function in Shell Scripting
A function is a block of code that is reusable and performs certain operations. Like any other programming language, Shell-Scripting also supports functions. Functions are popular for the following reasons:
- Help to reuse a piece of code.
- Improve the readability of the program.
- Make the program modular.
- Make maintenance easier.
There are two ways of writing functions:
- Keep the function in the same script where it is used.
- Write a library of useful functions inside a file and import the file in the script where it is required.
Types of Functions
A function can be used to perform various operations in a shell script.
Syntax:
function_name ()
{
…
<statements>
…
}
In order to call a function, we need to write its name and pass the required parameters in a space separate fashion:
$ function_name $arg1 $arg2 $arg3
Now, let’s see different types of functions (on the basis of what operation they perform)
1. Functions that return a value to the calling section of the script using ‘return’ keyword. You can make certain calculations and return the result
2. Functions that terminates the shell script using the ‘exit’ keyword. You can use the exit keyword to terminate the script based on certain conditions.
3. Functions that change the value of a variable/variables. You can assign/change the value of a variable declared outside the function block. An important thing to note here is that if you declare a variable outside a function, it will behave like a global variable and can be accessed inside a function.
4. Functions that echo output to the standard output. You can print anything to stdout inside the function.
Please note that you need to define the function before calling it and this means you cannot define a function anywhere in the script.
Examples of Different Types of Function in Shell Scripting
Now let’s check some examples of different types of function described in the previous section:
Example #1
To calculate and return the average of numbers passed as argument:
Code:
find_average() {
sum=0
i=1
len=$#
x=$((len + 1))
while [ $i -lt $x ]
do
arg=${!i}
sum=$((sum + arg))
i=$((i + 1 ))
done
avg=$((sum / len))
return $avg
}
find_average 10 20 30 40
echo $?
Output:
Example #2
To terminate the script when the value of a certain variable becomes negative.
Code:
check_negative()
{
a=5
while :
do
if [ $a -lt 0 ]; then
echo "terminating the script"
exit 5
fi
a=$((a - 1))
done
}
check_negative
Output:
Example #3
To assign the sum of the numbers passed as parameters to a variable.
Code:
a=0
add(){
sum=0
i=1
len=$#
x=$((len + 1))
while [ $i -lt $x ]
do
arg=${!i}
sum=$((sum + arg))
i=$((i + 1 ))
done
a=$sum
}
add 5 4 9 1
echo $a
Output:
Example #4
To echo the sum of the numbers passed as parameters:
Code:
add(){
sum=0
i=1
len=$#
x=$((len + 1))
while [ $i -lt $x ]
do
arg=${!i}
sum=$((sum + arg))
i=$((i + 1 ))
done
echo $sum
}
add 10 15 25
Output:
Conclusion – Function in Shell Scripting
We have seen how we can use functions to do various operations like returning a value, terminating the program, assigning value to variables and printing output to stdout.
Recommended Articles
We hope that this EDUCBA information on “Function in Shell Scripting” was beneficial to you. You can view EDUCBA’s recommended articles for more information.