Table of contents
- Introduction
- How to create functions which parses arguments in bash scripting
- How to use functions with return statement
- Functions with redirections
- How to use local variables and global variables in functions
- Conclusion
Introduction
In our earlier article on simple functions in bash scripting we discussed the different ways we can create a function in your scripts .
This Article is the continuation of if , which explores some complex bash functions with different feature sets like passing arguments , using redirection , using return statement and exploring what is a local variable / global variable for a bash function .
If you have missed our earlier article , check this out
How to create functions which parses arguments in bash scripting
In the following script example , we have created a function named complex_function. This function , allows to parse arguments up to two passed while calling the function. For simplicity and easy understanding of the concept the code just checks whether there is an argument passed or not. We can mode code statements based on your use case after the condition is passed
Here are the things used in the code ,
- “$1” – To call the first argument passed
- “ $2” – To call the second argument passed
- [ -z “$1” ] – conditional to check whether the passed argument is a empty string or not , if there is no argument passed it will treat is as the empty string and returns True satisfying the condition
#script
[root@discoveringsystems Bash-functions]# cat bash-complex-functions
#!/bin/bash
complex_function () {
if [ -z "$1" ]
then
echo "there is no argument passed in 1st position"
else
echo " the argument passed in first position is '$1' "
fi
if [ -z "$2" ]
then
echo " there is no argument passed in 2nd position"
else
echo " the argument passed in the second position is '$2' "
fi
}
echo " first function call with two arguments passed "
echo "-----------------------------------------------"
complex_function 1 red
echo ""
echo " second function call with one argument passed "
echo "-----------------------------------------------"
complex_function 1
echo ""
echo " third function call with no arguments passed"
echo "---------------------------------------------"
complex_function
#Script execution
[root@discoveringsystems Bash-functions]# ./bash-complex-functions
first function call with two arguments passed
-----------------------------------------------
the argument passed in first position is 1
the argument passed in the second position is red
second function call with one argument passed
-----------------------------------------------
the argument passed in first position is 1
there is no argument passed in 2nd position
third function call with no arguments passed
-----------------------------------------------
there is no argument passed in 1st position
there is no argument passed in 2nd position
How to use functions with return statement
Like other programming languages , Bash also supports the use of return statements in the functions , but the return statement in bash can return only the exit status . In bash , exit status of “0” means success , we can have a value up to 255. With no return statement , the exit status is pulled from the last command execution part of the script. Calling the return statement exits the function at that point , so we need to use it only when the function code block is over. To return string or other values after the function execution is over, we can use the echo to render the final output as per expectation. To view the exit status of a code blocks like functions , we can use this “$?“
Functions without return statement
In the following example code , function add_numbers is adding two numbers parsed as arguments and since we didn’t use any return statement at the end of the function the exit status is pulled from the last command execution part of the function , which seems to be success as we see 0
#script
[root@discoveringsystems Bash-functions]# cat bash-function-return
#!/bin/bash
add_numbers () {
after_addition=$(($1 +$2))
echo " $1 + $2 equals to $after_addition "
}
add_numbers 3 6
echo "exit status is $?"
#script execution
[root@discoveringsystems Bash-functions]# ./bash-function-return
3 + 6 equals to 9
exit status is 0
Functions with return statement to set exit status as per use case
In this example , on the same script we are using the return statement to set non zero exit status intentionally , even though the function didn’t have any errors and the code block execution was fine . For your information exit status 1 means “Operation not permitted” which is an error
#script
[root@discoveringsystems Bash-functions]# cat bash-function-return
#!/bin/bash
add_numbers () {
after_addition=$(($1 +$2))
echo " $1 + $2 equals to $after_addition "
return 1
}
add_numbers 3 6
echo "exit status is $?"
#script execution
[root@discoveringsystems Bash-functions]# ./bash-function-return
3 + 6 equals to 9
exit status is 1
Functions with return statement cannot return value with more than 255
In the same script , this time we are trying to return a value of 450 and instead we got exit status as 194 .Exit status of 194 is also an error . This is due to the reason we cannot cannot return any values out of these range ( 0-255 ) , to get the stdout as per your wish , you can use the echo statements to print a final result part of the function
#script
[root@discoveringsystems Bash-functions]# cat bash-function-return
#!/bin/bash
add_numbers () {
after_addition=$(($1 +$2))
echo " $1 + $2 equals to $after_addition "
return 450
}
add_numbers 3 6
echo "exit status is $?"
#script execution
[root@discoveringsystems Bash-functions]# ./bash-function-return
3 + 6 equals to 9
exit status is 194
Functions with redirections
We can also use redirections to provide stdin for the function code block , in the following example we will be using the while read to read the lines one by one from a text file redirected to the code block of the function and save it to a variable for further use in the script for that iteration
#text file used so that while read can read lines one by one
[root@discoveringsystems Bash-functions]# cat book.txt
this is the first line
this is the second line
this is the third line
#script
[root@discoveringsystems Bash-functions]# cat bash-function-redirection
#!/bin/bash
read_book () {
{
while read Parsed_text
do
echo "$Parsed_text"
done
} < book.txt
}
read_book
#script execution
[root@discoveringsystems Bash-functions]# ./bash-function-redirection
this is the first line
this is the second line
this is the third line
How to use local variables and global variables in functions
In this following example , we have created a local variable named var_1 which can be used only within the function code block , we cannot call it externally , if it is called outside the function it will return none. Also in the same script we declared one global variable named var_2 which can be called both inside and outside the function block
#script
[root@discoveringsystems Bash-functions]# cat bash-function-variables
#!/bin/bash
variable_function () {
local var_1="red"
echo " local variable var_1 $var_1 is valid only inside func"
echo " var_2 $var_2 is declared outside , still usable inside func"
}
var_2="blue"
variable_function
echo " var_2 $var_2 can be used even outside func"
echo " var_1 $var_1 cannot be used outside the func"
#script execution
[root@discoveringsystems Bash-functions]# ./bash-function-variables
local variable var_1 red is valid only inside func
var_2 blue is declared outside , still usable inside func
var_2 blue can be used even outside func
var_1 cannot be used outside the func
Conclusion
In this article we learned about using different features in the functions part of the bash script. For more articles on bash scripting you can go through our bash scripting course. Link below