Shell Scripting Tutorial: Shell Script Functions

A function is a way to break up a big program into smaller modules. By doing so the program will be easier to debug and easier to understand. It also helps to reuse the code.

How to create a function?

Syntax:

<function_name>()
{
	#statements
}
Example:
exampleFunction()
{
	echo "This is an example function"
}

How to call a function?

A function can be called by specifying it’s name.
Full example:
#!/bin/bash

#defining a function
fun() 
{
	echo "This is an example function"
}

#calling a function
fun

Output:

This is an example function

Below are the different types of function shell script supports:

1. Function with no return value and no input arguments

This is a simple function. It will not take input arguments and doesn’t return any value.

Example:

#!/bin/bash

#defining a function
fun() 
{
	echo "This is a simple function"
}

#calling a function
fun

Output:

This is an example function

2. Function with return value and no input arguments

In shell script, returning a value from a function can get tricky. Usually in shell script, “return” is considered as “exit” status. It takes value from 0 to 255. Hence to return a value use echo. And save the return value inside a variable while calling the function.

If your return value is 0 to 255 you can use “return” statement. Another point to note is, while returning a variable using “return” keyword, only integer value from 0 to 255 can be returned. String values cannot be returned.

Hence it is recommended to use echo command to return the values.

Example:

#!/bin/bash

#defining a function
fun() 
{
	return 1;
}

#calling a function
fun 
var=$? #$? is used to get the previous value
echo "The return value using return keyword is $var"


fun_1() 
{
	echo "hello world"
}
#calling a function
var_1=$( fun_1 )

echo "The return value using echo keyword is $var_1"

Output:

The return value using return keyword is 1
The return value using echo keyword is hello world

3. Function with input arguments

In the previous example we saw how to correctly return a value from a function. In this example we shall see how to pass arguments to a function.

While calling the function you can specify the arguments as,

function_name “$arg1” “$arg2”

Then the function will identify the arguments by their position not by the name. i.e $1, $2, and so forth. $0 will be the name of the script itself.

Example:

#!/bin/bash

#defining a function
fun() 
{
	echo "The first variable is $1"
	echo "The second variable is $2"
	echo "The third variable is $3"
}

#calling a function with arguments
fun "1" "2" "3"

Output:

The first variable is 1
The second variable is 2
The third variable is 3

4. Nested Functions

Nesting is not same as we do it other languages. Nesting can be bit difficult at first. Below I have explained as simple as possible.
In shell script nesting can be done in 2 ways:

Method 1: True nesting

In this method, instead of using “{ }” use “( )”. By using this method, the inner function can be used inside the outer function. You cannot pass the inner function elsewhere.

Example:

#!/bin/bash

#defining a function
outer_function() 
(
	echo "In outer function"
	
	inner_function()
	{
		echo "In Inner Function"
	}
		
	#calling inner function
	inner_function
) #note we have used ( ) in outer function instead of { }

outer_function

Output:

In outer function
In Inner Function

Method 2: Indirect nesting

#!/bin/bash

one() 
{
  echo "One"
}

two ()
{
  echo "Two"
}

#this can be considered as outer function.
three ()
{

   echo "Three"
  
#calling the first two function
   one
   two
}

#calling third function
three

Output:

Three
One
Two

Local Variables in function:

In shell script, by default all the variables are global variables. Hence modifying a value inside a function will modify that variable globally. This will create issues. Hence to make a variable local to that function use “local” keyword.

Example:

#!/bin/bash

func_2()
{
	global_var=20
	echo "The value of global before change is $global_var"
	
	local local_var=20
	echo "The value of local variable is $local_var"
}
func_2
	
	#manipulating the global variable outside the function
	global_var=$(( $global_var+1 ))
	echo "The value of global after change is $global_var"

Output:

The value of global before change is 20
The value of local variable is 20
The value of global after change is 21

As you can see from the output, even-though we declare “global” variable inside the function, we can manipulate it outside the function also.

Read only functions.
We can make a function readonly by adding “readonly -f” option while calling a function. This is useful when you try to define another function which has already been defined.

Example:

#!/bin/bash

func_1()
{
	echo "This is function 1"
}
readonly -f func_1

#calling func_1
func_1

#trying to define another function with same name

func_1()
{
	echo "Defining function 1 another time"
}

Output:

This is function 1
18_function.c: line 17: func_1: readonly function
Write a Comment

Leave a Comment

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