SHELL FUNCTION – In this tutorial, you will learn function in bash script, function in shell script, function in shell script with arguments, shell script function with parameters and return value with examples.
- Shell program supports the function which is used to implement the variables as well as execute the linux commands
- Shell scripts two ways to create a function in linux. They are
- Without using function keyword
- With using function keyword.
Syntax 1 – Without using reserved function keyword
- Here no need to use reserved keyword function to create a function
- You can directly create a function by just specifying the user defined name followed by () operator.
Example
Syntax 2 – With using reserved keyword function
Here, function can be defined using the reserved word function.
Example
CALLING FUNCTION
- Shell function can be called using its name only.
- The operation () should not be used while calling the function.
I. EXAMPLE OF SHELL FUNCTION
1. SOURCE CODE
2. OUTPUT
SHELL FUNCTION WITH ARGUMENTS
- Shell function supports the arguments
- The arguments are given after the function name while calling the function
- Each argument is separated by space
- The positional parameters like $1, $2, etc… will receive the values of function arguments inside the function definition.
II. EXAMPLE OF SHELL FUNCTION WITH ARGUMENTS
1. SOURCE CODE
echo “——————————————-“
echo “\t\tShell Function with Arguments”
echo “——————————————-“
# creating a shell function
disp()
{
a=$1
b=$2
rs=`expr $a + $b`
echo “Sum is: $rs”
}
# calling function with arguments
disp 12 21
2. OUTPUT
SHELL FUNCTION WITH RETURN STATEMENTS
- Like c language, shell function returns the values
- The return keyword is used to return the values in the shell function
- The returned result of the calling function can be obtained using the special symbol $? (by default, the return values of the function will be stored in the built-in variable $?)
NOTE
- It is important to note that the shell function will return a single value.
III. EXAMPLE OF SHELL FUNCTION WITH RETURN VALUE
1. SOURCE CODE
echo “————————————————————-“
echo “\t\tShell Function with Return Value”
echo “————————————————————-“
a=12
b=23
rs=0
# shell function
disp()
{
rs=`expr $a + $b`
# return the variable
return $rs
}
# calling function
disp
# get the return value from function using $?
k=$?
echo “Sum is: $k”
2. OUTPUT
MORE TUTORIALS
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…