How to Call a Function in Bash [7 Cases]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Calling Bash functions is as simple as invoking a Bash command. To call an already-declared Bash function, use the function name as if it were a Bash command. For instance, to invoke a function declared as fname() { command; }, type fname in the terminal or within the script where the function is defined. This article talks about 7 different cases of calling functions in the Bash shell.

1. Calling a Function Defined in the Terminal

Think about a function defined in the terminal as follows:

func1 () {
echo "Hello from a Bash function"
}

Defining a function in the terminalTo call the function write the function name like a Bash command. For instance, just writing the function name func1 executes the ”func1” function defined in the terminal.Calling a function defined in the terminal

2. “source” Command to Call Function Written in a Script

A function written in a Bash script can be called using the source command. Say you have a script that contains a function. To call the function first source the script by the source command. Then write the function name like a Bash command:

#!/bin/bash
function greet()
{
        echo "Welcome to LinuxSimply"
}
source welcome.sh

After sourcing the welcome.sh file, writing “greet” calls the greet function defined in the welcome.sh file.

greet

Calling a function using the source commandThe output shows the message “Welcome to LinuxSimply” which is written inside the function.

3. Calling Functions Inside Bash Script File

One can invoke a function like a bash command inside the bash script that contains the function. After that follow the usual steps to execute the script. But make sure the function definition precedes the function call.

#!/bin/bash
function greet()
{
        echo "Welcome to LinuxSimply"
}
# function call
greet

In the last line of the script, greet invokes the function “greet“ defined in the script.

To make the bash script executable, use the below code:

chmod u+x welcome.sh

Replace the filename with your filename. Finally, execute the file to see the output of the function:

./welcome.sh

Calling a function in Bash scriptUpon execution, the script shows the output of the echo command written inside the function.

4. Function Calling As an Argument of a Bash Script

Calling functions as an argument of a script is an elegant technique. This is super useful when users have multiple functions in a script. To call a function supply the function name as an argument while executing the script:

#!/bin/bash
function f1()
{
   echo "Hello Linux Users"
}
function f2()
{
   echo "This is a Bash function."
}
function f3()
{
   echo "This is another Bash function."
}
f_call=$1; shift; $f_call "$@"
EXPLANATION

The script contains f1, f2 and f3 functions. The variable f_call is assigned the value of the first command-line argument ($1), which should be the name of the function to be called.

The shift command is used to shift the command-line arguments to the left, effectively discarding the first argument (the function name). Finally, the specified function ($f_call) is called with any remaining command-line arguments (“$@”). This allows users to pass additional arguments to the chosen function.

Calling function as argument of an scriptIn the first execution, the script is executed and calls the f1 function cause f1 is provided as the first positional argument.

In the second execution, nothing is provided as a positional argument. Hence no function is called.

At last, the third execution calls the function f3 and shows its output.

5. Calling a Function in a Script from a Separate Script

Use the source command or the . (dot) operator to call a function defined in one script from another script in Bash. This allows you to include the content of one script into another, making the functions from the first script available in the second one:

Script 1 >

#!/bin/bash
function greet() {
    echo "Hello from script1.sh"
}

Script 2 >

#!/bin/bash
# Source the first script to make its functions available
source script1.sh

# Call the function from the first script
greet
EXPLANATION

Here, the script1.sh contains a function greet. script2.sh sources the script1.sh by the source command. After sourcing the first script, the function “greet“ is called by its name.

Calling function of separate script in a scriptExecution of script2.sh shows that the “greet“ function of script1.sh is available and properly called in the second script.

6. Calling a Function Inside Another Function

To call a function inside another function, simply write the function name within the body of the main function:

#!/bin/bash
f1 ()
{
echo "This is the first function."
# Calling f2 inside f1
f2
}

f2 ()
{
echo "This is the second function."
}

# Calling f1
f1
EXPLANATION

The bash script defines two functions f1 and f2. The f2 function is called inside the f1 function.

Calling a function inside another functionOnce f1 function is called it executes both f1 and f2 as you can see in the image above.

7. Calling Function With Arguments in Bash

Provide the argument after the function name when calling the function. The “welcome“ function of the following script requires an argument for proper execution:

#!/bin/bash

welcome () {
  echo "Hello $1. Welcome to LinuxSimply"
}

welcome "Anita"
EXPLANATION

welcome "Anita" calls the welcome function with the argument “Anita”. The function utilizes this argument when it uses the positional parameter $1 to refer to the argument.

Calling function with arguments in BashThe program successfully prints a welcome message utilizing the argument “Anita”.

To supply multiple arguments to a function place them one after one separated by a space.

Conclusion

In conclusion, calling a Bash function is straightforward yet important. While it is easy to invoke a function, the context in which a function is called may vary. Regardless of the scenario, users must verify that functions are defined before attempting to call them. This simple precaution ensures a smooth execution and helps prevent errors while calling a function.

People Also Ask

How to store function output to a variable?

To store function output to a variable, use command substitution. Enclose the function name with parenthesis and put a dollar sign before the starting parenthesis like- $(func_name). Then assign it to a variable such as- result=$(func_name). The “result“ variable contains the output of the function.

Can I call a function from a script rather than executing the whole script?

Yes, you can call a specific function from a script without executing the entire script. You can adapt various techniques to achieve that. The common approach is passing the function name as an argument.

How do you call a function within an if statement in Bash?

To call a function within an if statement in Bash, you can use the following syntax:

if function_name; then
   # codes
fi

This ensures that your function returns an exit status (0) for the if condition to be true, indicating successful execution.

Why does invoking a function show that the function doesn’t exist?

If invoking a function shows that the function doesn’t exist, there could be several reasons for this issue. Typically this issue is raised when a function is defined after attempting to invoke it.

Related Articles


<< Go Back to Bash Functions | Bash Scripting Tutorial

5/5 - (1 vote)
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment