Getting started

Hi people! today I am going to show you how and when to use functions with simple examples. So, what is function in BASH and when should I use it? It is nothing but set of codes that allows you to call from anywhere with-in a script which makes your work simple 🙂 Say if you are going to repeat same algorithm for ‘n’ number of times, you can just call function to save your time 😎

Syntax

There are two types of syntax available for functions. Both will do the same and it completely depends on the programmer 😉 There is nothing different other than their syntax, so you can follow your convenient one 😀

function – syntax #1 :

1
2
3
function yourFunctionName {
   < your code here >
}

function – syntax #2 :

1
2
3
yourFunctionName () {
   < your code here >
}

Function types

We can barely classify functions into two types as functions without arguments and functions with arguments. Besides these classification, return values & local variable concepts are common in functions.

Explanation

In functions without arguments, it is straight forward . You simply call functions without arguments. Whereas in functions with arguments, arguments are being passed while calling it. Nevertheless their syntax remains same. Only function calling method differs 😎

Local variable

Local variable is nothing but it stores the data temporarily for a variable. i.e., assigned variable is valid only with in the function. In general local variables are used to minimize code length and complexities

Return value

Each and every operations in BASH has return value by default. The fact is we neglect them 😉 So you might raise a question what is the purpose of using return value in real time. If you are automating something and you want to proceed next step based on previous output, then you may need this 💡 Also you can use return value as flag option in conditions.

By default, return value throws zero for successful executes and non-zero integers for non-successful executes.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
#Functions without arguments
fun1() {
echo "Inside function block 1 -- before execution -- $var1"
var1="variable 2"
echo "Inside function block 1-- after execution -- $var1"
}

#Functions with arguments
fun2() {
echo "prints the arguments passed along with function -- $1"
echo "Inside function block 2 -- before execution -- $var1"
local var1="variable 3"
echo "Inside function block 2 -- after execution -- $var1"
return 10
}

#Program starts here
var1="variable 1"
echo "before passing variables into functions -- $var1"
fun1
echo "after executing function block 1 with return value $? -- $var1"
fun2 "argument 1"
echo "after executing function block 2 with return value $? -- $var1"

Result

1
2
3
4
5
6
7
8
before passing variables into functions -- variable 1
Inside function block 1 -- before execution -- variable 1
Inside function block 1-- after execution -- variable 2
after executing function block 1 with return value 0 -- variable 2
prints the arguments passed along with function -- argument 1
Inside function block 2 -- before execution -- variable 2
Inside function block 2 -- after execution -- variable 3
after executing function block 2 with return value 10 -- variable 2

Hope you all had clear vision on functions. Still getting trouble in these concepts? let us know through comment section. We are happy too help you ?

Leave a Reply

avatar
  Subscribe  
Notify of