How to use functions in Bash Scripting

This post will look at how you can use functions in Bash Scripting. Head ups! You may want to read the bash scripting basics post if you are new to Bash scripting.

A function is a set of commands used to perform a particular task. There are many benefits to using functions. For instance, you can use them to avoid code repetition, to decrease the error chances, and to increase the readability of the program.

Another benefit of using them is that you can easily change the code according to your needs. It is also a good practice to use functions as they provide reusability, which means that you do not have to write the same code again and again.

You can call a function as many times as you need its functionality.

The syntax of the function is as follows:

name_of_function(){
  One or more commands
}

How to define and call a Bash function?

Before we see examples of functions, keep in mind that we write all our commands between a pair of curly braces which is the body of the function. A function must be defined before you give a call to function.

Now, let’s make a script named script.sh and see how you can create and use functions.

#!/bin/bash

# defining a function
hello_func(){
    echo "Hello World!";
}

# Calling the function
hello_func

As you can see, first, we have defined the Bash function, and then we called this function. We only have to write the name of the function to call it from any part of the script.

Now, let’s run this script to see the results of this function.

chmod +x script.sh
./script.sh

After we run this script, we will see the following output:

Hello World!

How to pass and access arguments in Bash functions?

The function in the above script does not take any argument. An argument is something you pass to the function while calling it. Now, let’s see through a script how we can pass and access arguments in a function. Let’s again name this script as script.sh.

#!/bin/bash

# Function Definition
get_arguments(){
  echo "Hello World!"
  echo "1st argument is : $1"
  echo "2nd argument is : $2"
  echo "3rd argument is : $3"
}

# Function Call
get_arguments scripting 3366 hi

When we run this script, it gives the following output:

Hello World!
1st argument is : scripting
2nd argument is : 3366
3rd argument is : hi

As you can see, we have passed three arguments to the function, which are scripting, 3366, and hi. We always use a space to separate the arguments as you can see from the function call.

If you want to pass an argument that contains spaces, then you need to surround it with double-quotes like this:

get_arguments scripting 3366 “hi, how are you?”

Now, again we have passed three arguments to the function. Double-quotes surround the third argument.

How to store arguments passed to functions in variables?

In the above script, when we passed the arguments to the function and accessed them directly. We can also store the arguments passed to function in the variables. Let’s see an example script.

#!/bin/bash

# Function Definition
get_arguments(){
  echo "Hello World!"
  echo "1st argument is : $1"
  echo "2nd argument is : $2"
  echo "3rd argument is : $3"
}

# Function Call
get_arguments scripting 3366 hi

If we run this script, we will get the following output.

Hello World!
1st argument is : scripting
2nd argument is : 3366
3rd argument is : hi

We can see from the save_arguments function that we have stored both arguments in the variables arg1 and arg2. Now, we can access the arguments passed using the variables arg1 and arg2 instead of accessing them directly.

How to return values from functions?

Whenever we want to return anything from the function, we use the echo statement. Let’s make a script named script.sh and return the addition of two numbers from a function.

#!/bin/bash

# Function Definition
add_numbers(){
opr1=$1
opr2=$2
sum=$(($opr1 + $opr2))
echo "$sum"
}

# Function Call
result=$(add_numbers 25 5)
echo "sum of numbers is : $result"

The result of this script would be:

sum of numbers is : 15

As you can see from this script that we used the echo statement at the end of this function. The echo statement in the function means that we want to return this value to a part of the program where this function is called.

As you can see, while calling the function, we wrote a line like this:

result=$(return_values)

Now, in this line, we are not only calling the function but also storing the value returned by the function in a variable named result. This is how we can access the value returned by the function.

Now, let’s see another example script named script.sh that covers both return values and arguments.

#!/bin/bash

# Function Definition
add_numbers(){
opr1=$1
opr2=$2
sum=$(($opr1 + $opr2))
echo "$sum"
}

# Function Call
result=$(add_numbers 25 5)
echo "sum of numbers is : $result"

When we run this script, it gives the following results:

sum of numbers is : 30

The function in this script takes two arguments during the function call and return the result after adding these two numbers.

You can call this function as many times as you want by passing the two numbers as arguments whenever you give a call to this function.

In Summary

We have learned how you can use Bash functions so that we can re-use existing code. We have also seen how you can pass and return values to functions.



About the Authors

Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic!

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



Leave a Reply

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