In our last tutorial, we discussed advanced uses of I/O redirection & we will now learn to create functions for our Bash / shell scripts. Learning how to create a function is an important skill required for BASH scripting.
When we are writing our scripts, we might see ourselves using a section of the script over and over again like using a loop that is checking a condition many times in a script. So rather than writing a section of the script over & over again, we will create that section of script as a Functions aka Bash Functions also.
Functions are like a mini-script inside the scripts. We will create a function with a name & whenever that function is required, we will just type the name of the function rather than typing the whole section of a script again & again.
Recommended Read: Bash Scripting: Learn to use Basic REGEX (Part 2)
Also Read: 3- BASH Scripting – Basic scripting & using variables in script
There are 2 ways in which we can define the bash functions in our scripts;
- The first method is like this,
function name {
commands
}
- The second way is more similar to how we declare functions in many programming languages
name () {
commands
}
Let's discuss an example of how we can use it.
Example script:-
#!/bin/bash
# creating a function in script
function print{
echo “Welcome $USER, this is a fucntion”
}
var=1
while [ $var –le 5 ]
do
var=$[ $var + 1 ]
done
echo “Function is now complete”
Now, here we created a function named “print” which we can then use as many times as we want by just typing print in our script.
As mentioned earlier, functions are mini-scripts inside our scripts so whatever things we can use with our scripts, we can use them with functions like variables, loops, etc. Here in our next example, we will create a variable called the local variable.
A local variable is created inside a function and the value of a variable can only be used inside the function even if we have a variable by the same name as our local variable, the local variable value will have no effect on the other variable.
Example:- function test {
Local var = $[ $value * 2 ]
out =$[ $var + 2 ]
}
Here, ‘var ’ is our local variable & by mentioning ‘local’ before var, we have made it a local variable. The value of var can only be used for this function & not outside the function even if there is another variable named var outside of the function.
Note:- It's always wise to use local variables for our functions. Global variables inside functions should only be used when extremely needed.
Example script :-
#!/bin/bash
# Script for checking functions with local varibles
a=34
b=9
sum () {
local a=$1
local b=$2
echo $(( $a + $b ))
}
echo "a: $a and b: $b"
echo "Calling sum() with a: $a and b: $b"
sum 6 13
echo "a: $a and b: $b after calling sum()"
echo $(( $a + $b ))
In this script, we first created two global variables a & b, and assigned them values. Then we created two local variables for our function sum with the same name as global variables i.e. a & b. Both the local & global variables were assigned different values & when we will run the script, you will notice that the values of both the variables don’t affect each other.
That’s it guys, this was our tutorial on creating & using functions in our bash / shell scripts. Please leave your comments/queries in the comment section below.
We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,
DigitalOcean - 100$ free credit & Linode - 100$ free credit
Check some Exclusive Deals, HERE.
Also, check out DevOps Book You should read section.