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.

YouTube player

Basics of a Bash function

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, decrease the error chances, and increase the program’s readability.

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 repeatedly.

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 function’s body. This is because you must define a function 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 function’s name 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: 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. But, again, 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. So 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. So, for example, 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)

In this line, we are calling the function and 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 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 returns 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.

Wrapping up

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.

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.