bash functions - Learn about Declaration, Scope and Arguments

When working with a bash script, functions are a very good option to reuse code. Functions make your scripts more readable and you can reuse the code again without writing the same code again and again.

Basically bash function is a set of commands that can be used for executing a single or group of commands again and again.

It allows you to break a lengthy code into small parts which will enable you to reuse and minimize the code.

In this tutorial, we will show you the basics of bash function and how they use in shell scripting.

Basic Syntax

The basic syntax of the bash function can be defined in two formats:

function_name() {
commands
}

And

function function_name() {
commands
}

Where:

  • function_name:
    It is the name of the function you want to declare.
  • commands:
    It is a list of commands that you want to execute when the function is called.

You can define a single line function in two formats:

function_name() { command;}

And

function function_name() { command;}

Let’s create a function.sh bash script to understand this better.

nano function.sh

Add the following code:

#!/bin/bash
my_function () {
echo 'Welcome to WebserverTalk.'
}
my_function

Save and close the file when you are finished.

Then, run the script using the following command:

bash function.sh

You should see the following output:

Welcome to WebserverTalk.

Passing Arguments in Bash Functions

In some cases, you need to pass the arguments and process the data in bash functions.

You can supply the arguments directly after the function name.

Let’s see a simple example.

nano function.sh

Add the following code:

#!/bin/bash
my_function () {
echo hello $1
echo hello $2
}
my_function hitesh rajesh

Save and close the file when you are finished.

Then, run the script using the following command:

bash function.sh

You should see the following output:

hello hitesh
hello rajesh

Variable Scope of Bash Functions

Scope refers to the visibility of variables.

By default, every variable has a global scope that means it is visible everywhere in the script.

You can also create a variable as a local variable. When you declare a local variable within the function body, it is only visible within that function.

Let’s take a simple example to understand this better.

nano function.sh

Add the following code:

#!/bin/bash
var1='P'
var2='Q'
my_function () {
local var1='R'
var2='S'
echo "Inside Function"
echo "var1 is $var1."
echo "var2 is $var2."
}
echo "Before Calling the Function"
echo "var1 is $var1."
echo "var2 is $var2."
my_function
echo "After Calling the Function"
echo "var1 is $var1."
echo "var2 is $var2."

Save and close the file when you are finished.

Then, run the script using the following command:

bash function.sh

You should see the following output:

Before Calling the Function
var1 is P.
var2 is Q.
Inside Function
var1 is R.
var2 is S.
After Calling the Function
var1 is P.
var2 is S.

Return Values in Bash Functions

In bash scripting, you can not return the process value at the end of the function when it is called.

However, you can set a return status of the function using the return statement.

When a bash function completes, it returns 0 for success or 1-255 range for failure.

Look at the following example.

nano function.sh

Add the following lines:

#!/bin/bash
my_function () {
echo "Welcome to WebserverTalk"
return 10
}
my_function
echo $?

Save and close the file when you are finished. Then, run the script using the following command:

bash function.sh

You should see the following output:

Welcome to WebserverTalk
10

You can also declare and use a global variable at the end of the function to return the value.

Let’s see the following example:

nano function.sh

Add the following lines:

#!/bin/bash
my_function () {
echo "Welcome to WebserverTalk"
result="Blog"
}
my_function
echo $result

Save and close the file when you are finished. Then, run the script using the following command:

bash function.sh

You should see the following output:

Welcome to WebserverTalk
Blog

Conclusion

As you see, the Bash Function is very useful for reusable code to perform a specific operation. Once it is defined, you can execute it several times in a script. We hope this tutorial has been helpful and if you have any questions, please ask in the comment section below!