• We did not send an email asking for donations - please read this post.

Bash 09 – Functions

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
265
Reaction score
253
Credits
6,739
We have covered all of Bash, except for Functions. Functions can be very helpful for a script.

If you have any programming experience, then you should already have an understanding of the Function. A Function is a set of commands that you utilize multiple times. We can set the Function up one time and called as many times as we need.

Structure

Each script can have multiple Functions, but we must lay them out as one of the following:

function_name() {
command1
command2
command3

….
command-n
}

function function_name {
command1

command2
command3
….
command-n
}

To call a function, just use the function name. For example, if we were to use ‘getopts’ and use the option ‘*)’ for handling all errors that are not managed elsewhere, we can display help information for script usage. We could set up a function that is used to display a script help message. We can also use the function when a user uses a parameter, such as ‘h’ or ‘-h’, to display help.

Basic Example

As I just mentioned, we can display a help message.

help_message () {
echo “The proper usage of the script is:”
echo “ ”
echo “ test.sh <parameters>”
echo “ ”
echo “ -h – this help information”
echo “ -d – date”
echo “-t – time”
}


The example is a very basic one to only produce display output. There is no extra processing done in this case.

Variables

There are two different types of variables in any script. The types are ‘global’ and ‘local’.

Global variables are those that exist within the whole script, including the function.

Local variables exist in the function.

Let’s look at the following example:

f_test() {
local var1=7
var2=9
echo "In Function"
echo $var1
echo $var2
echo $var3
}

var1=1
var2=2
var3=5
echo "Before Function"
echo $var1
echo $var2
echo $var3
f_test
echo "After Function"
echo $var1
echo $var2
echo $var3


In this example, we have a function named ‘f_test’. The script starts at the first line after the function. At the start, we set three variables. The variables and their values are ‘var1’ to ‘1’, ‘var2’ to ‘2’ and ‘var3’ to ‘5’.

We print the three variables out before the function is called. The values are ‘1’, ‘2’, and ‘5’.

The function is then called. Variable ‘var1’ is locally changed to a value of ‘7’. The variable ‘var2’ is changed globally to ‘9’ and ‘var3’ is left unchanged. We print the values out as ‘7’, ‘9’ and ‘5’.

We then go back to the main script and print the values again. The values are now ‘1’, ‘9’ and ‘5’.

The actual output is:

Before Function
1
2
5
In Function
7
9
5
After Function
1
9
5


This should give you an understanding to manipulate variables within a script and within the function.

Passing Arguments

You can pass arguments to a function to be processed or change how it processes items.

Any variables passed to a function can be accessed as $1, $2, $3, etc. These are not the same as the $1, $2, etc. variables sent to the script.

func_test () {
echo $1
echo $2
echo $3
}

func_test $1 $2 Hello
echo $3


Let’s call the script with the arguments of ‘1’, ‘2’ and ‘3’.

At the beginning, we are calling the function with 3 arguments. The first two are $1 and $2. These are the values sent to the script (‘1’ and ‘2’). The third argument is the word ‘Hello’.

Within the script, we are echoing the value for the argument ‘$1’, which is ‘1’. This is the argument we sent when calling the script. It echoed the second variable as a value of ‘2’. Again, this is sent from executing the script. The third argument is ‘Hello’, which is printed on the screen.

After the function finished, we go back to the main body of the script and echo out the third argument, which is ‘3’.

We show the actual output as:

./function3.sh 1 2 3
1
2
Hello
3



The values of the arguments is the same as the arguments passed to a script. The ‘$0’ is the function name. Each argument is ‘$1’, ‘$2’ and so on. The value in ‘$#’ is the number of arguments.

Resulting Values

Like a script can have an exit value, a function can as well.

At the end of a function, you can cause a resulting value with the command ‘result <value>’. Say, for instance, the function was calculating a value, you can set the value to the ‘result’. From the main body of the script, you can retrieve the value with the variable ‘$?’.

If you use a resulting value, you must copy the value to a variable before you call another function or it overwrite the value in the variable ‘$?’.

Sending a Value from a Function

If you want to send a value back from a function, you can echo the value and save it to a variable. Since the value is being placed into a variable, it will not echo to the display. We give an example below:

test_function() {
echo $(($var1+$var2))
}

var1=1
var2=5
result=$(test_function)
echo $result


The value is echoed in the function, but we set the function equal to the variable ‘result’.

You can use this method instead of placing a value into a global variable, which would work as easily, but sometimes if you have a long script, you may not want to take a chance of using the wrong variable. The preference is up to the creator.

Conclusion

Hopefully, you have a good grasp of Functions. Functions should help you reuse code that is the same and being repeated.

Functions can help reduce code when lines are being repeated in a script. If a section is complicated, you can also make it a function to separate it from the main body and work on it by itself.

For anyone who may have functions they can use in multiple scripts, you can keep a function file that you can copy out functions as you need them.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Latest posts

Top