Functions

Bash Scripting - Working with Functions

What are functions?


Functions are basically a pre-defined routine that can be called from within a script or from a command line. Functions are very useful when used within scripts. This allows you to utilise the same piece of code without having to define it multiple times within your script.

In its simplest form, a function can be created as follows:



Define a Function



function myfunction() { ls -lF; }

The above function "myfunction" can now be invoked simply by typing the function name at the command line:



john@john-desktop:~/scripts$ myfunction
total 20
-rwxrwxr-x 1 john john  142 Nov 11 11:29 here1.sh*
drwxrwxr-x 2 john john 4096 Nov 11 11:26 shellscripts/
-rwxrwxr-x 1 john john   35 Nov 11 11:47 string1.sh*
-rwxrwxr-x 1 john john   88 Nov 11 11:52 string2.sh*
drwxrwxr-x 2 john john 4096 Nov 10 20:57 textfiles/

Multiple commands can be used within a function. These are simply separated by a semi colon:


Define a Function



function myfunction() { ls -lF; echo "Welcome to the Land of Linux"; }


john@john-desktop:~/scripts$ myfunction
total 20
-rwxrwxr-x 1 john john  142 Nov 11 11:29 here1.sh*
drwxrwxr-x 2 john john 4096 Nov 11 11:26 shellscripts/
-rwxrwxr-x 1 john john   35 Nov 11 11:47 string1.sh*
-rwxrwxr-x 1 john john   88 Nov 11 11:52 string2.sh*
drwxrwxr-x 2 john john 4096 Nov 10 20:57 textfiles/
Welcome to the Land of Linux

As we can see both commands are now executed when our function is invoked.


How to view a Defined Function


To view a defined function you can use the "declare -f" command:



john@john-desktop:~/scripts$ declare -f
.....
.....
dequote ()
{
    eval echo "$1" 2> /dev/null
}
myfunction ()
{
    ls --color=auto -lF;
    echo "Welcome to the Land of Linux"
}
quote ()
{
    echo \'${1//\'/\'\\\'\'}\'
}
.....
.....

The above is only a small extract from the output. Here we can see that our function is defined.


Remove a function


To remove a function we can use the command unset -f myfunction:



unset -f myfunction

If you now display the known functions using the command "declare -f", you will see that the function has now gone. For more information regarding functions from the command line, see our Linux Fundamental section Alias and Functions.



Using functions within a script


Functions can be defined and used within scripts quite easily. To define a function within a script you can either define a path to where the function can be found or define the function at the beginning of the script:

Function called within a bash script



#!/bin/bash
#
#Define Functions
lol_test() {
ls -lF
echo "Welcome to the Land of Linux"
}

echo "Now calling our test function lol_test"
lol_test

echo "Calling our test function again..."
lol_test

Output from the above script



john@john-desktop:~/scripts$ ./function1.sh 
Now calling our test function lol_test
total 312
-rwxrwxr-x 1 john john    198 Nov 11 12:46 function1.sh*
-rwxrwxr-x 1 john john    142 Nov 11 11:29 here1.sh*
drwxrwxr-x 2 john john   4096 Nov 11 11:26 shellscripts/
-rwxrwxr-x 1 john john     35 Nov 11 11:47 string1.sh*
-rwxrwxr-x 1 john john     88 Nov 11 11:52 string2.sh*
-rw-rw-r-- 1 john john 294070 Nov 11 12:31 test.txt
drwxrwxr-x 2 john john   4096 Nov 10 20:57 textfiles/
Welcome to the Land of Linux
Calling our test function again...
total 312
-rwxrwxr-x 1 john john    198 Nov 11 12:46 function1.sh*
-rwxrwxr-x 1 john john    142 Nov 11 11:29 here1.sh*
drwxrwxr-x 2 john john   4096 Nov 11 11:26 shellscripts/
-rwxrwxr-x 1 john john     35 Nov 11 11:47 string1.sh*
-rwxrwxr-x 1 john john     88 Nov 11 11:52 string2.sh*
-rw-rw-r-- 1 john john 294070 Nov 11 12:31 test.txt
drwxrwxr-x 2 john john   4096 Nov 10 20:57 textfiles/
Welcome to the Land of Linux

From the above output we can see that the function "lol_test" was called several times. The function was called simply by specifying the name of the function. Although the use of functions within a script is useful, you may wish to create a function library where you can store useful functions. This again is easy to achieve:


Create a Function Library


It is very useful to create a library of functions that can be called by your scripts. This saves a lot of time if some of the functions need to be shared between multiple scripts. To create a function library we simply create a shell script with our functions. This shell script can then be referenced by other scripts. In the example below we create three simple functions in the usual manner.

Function Library



#!/bin/bash
#
#Define Script Functions

lol_test() {
echo "Calling lol_test function"
ls -lF
}

echo_test() {
echo "Calling echo_test function"
ls -l | wc -l
}

my_test() {
echo "Calling my_test function"
ls | wc -w
}

To use the above function library, we simply have to specify the path within our calling script:



#!/bin/bash
#
# Path to our Function Library
. /home/john/scripts/function_lib.sh

To invoke the functions within this library, we simply call them by name:



#!/bin/bash
#
# Path to our Function Library
. /home/john/scripts/function_lib.sh

echo "Lets call a function.."

lol_test

echo "And another one..."

echo_test

echo "And another Function"

my_test

In the above script we are calling the functions "lol_test", "echo_test" and "my_test". The output from running this script is as follows:

Output from above script



john@john-desktop:~/scripts$ ./function3.sh 
Lets call a function..
Calling lol_test function
total 320
-rwxrwxr-x 1 john john    198 Nov 11 12:46 function1.sh*
-rwxrwxr-x 1 john john    199 Nov 11 13:09 function3.sh*
-rwxrwxr-x 1 john john    218 Nov 11 13:03 function_lib.sh*
-rwxrwxr-x 1 john john    142 Nov 11 11:29 here1.sh*
drwxrwxr-x 2 john john   4096 Nov 11 11:26 shellscripts/
-rwxrwxr-x 1 john john     35 Nov 11 11:47 string1.sh*
-rwxrwxr-x 1 john john     88 Nov 11 11:52 string2.sh*
-rw-rw-r-- 1 john john 294070 Nov 11 12:31 test.txt
drwxrwxr-x 2 john john   4096 Nov 10 20:57 textfiles/
And another one...
Calling echo_test function
10
And another Function
Calling my_test function
9