13- BASH Scripting – Creating FUNCTIONS in script

In our last tutorial, we discussed about advanced uses of I/O redirection & we will now learn to create functions for our scripts. Learning how to create a function is an important skill required for BASH scripting.

When we are writing our scripts, we might see our ourselves  using a section of 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 script over & over again, we will create that section of script as a Function.

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 then typing whole section of a script again & again.

There are 2 ways in which we can define the functions in our scripts;

  • function name {

                                     commands

                                        }

  • Second way is more similar to how we declare functions in many programming languages

name () {

                             commands

                              }

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
print
var=$[ $var + 1 ]
done
echo “Function is now complete”

Now, here we created a function named “print” which we can then use as many time as we want by just typing print in our script.

As mentioned earlier, functions are mini-script 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 local variable.

A local variable is created inside a function and the value of variable can only be used inside the function even if we have a variable by the same name as our local variable, 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:- Its 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 firstly created two global variables a & b and assigned them values. Then we created two local variables for our function sum with 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 values of both the variables doesn’t affect each other.

 

That’s it guys, this was our tutorial on creating & using functions in our scripts. Please leave your comments/queries in comment section below.

 

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter – Make a contribution via PayPal

[paypal_donation_button align=”left” border=”1″]

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *