Using functions and positional parameters
Similar to other programming languages, function is a way to write a set of actions once and use it multiple times. It makes the code modular and reusable.
The syntax of writing a function is as follows:
function function_name { # Common set of action to be done }
Here, function is a keyword to specify a function and function_name is the name of the function; we can also define a function in the following ways:
function_name() { # Common set of action to be done }
The actions written within curly braces are executed whenever a particular function is invoked.
Calling a function in bash
Consider the following shell script that defines the my_func()function:
#!/bin/bash
# Filename: function_call.sh
# Description: Shows how function is defined and called in bash
# Defining my_func function
my_func()
{
echo "Function my_func is called"
return 3
}
my_func # Calling my_func function
return_value=$?
echo "Return value of function = $return_value...