Bash scripting is a powerful tool for automating tasks and creating complex workflows in the Linux environment. One of the key features of bash scripting is the ability to define and call functions. Functions allow you to encapsulate a set of commands and execute them as a single unit, providing modularity and reusability to your scripts. In this article, we will explore how to call a bash function and retrieve its return values.
Defining a Bash Function
Before we dive into calling a function and retrieving its return values, let’s first understand how to define a bash function. Functions in bash are defined using the following syntax:
function_name() {
# Function body
# Commands go here
# ...
}
You can replace function_name with the desired name for your function. The function body consists of the commands that will be executed when the function is called.
Returning Values from a Bash Function
In Bash, a function can return an exit status using the return statement. The return statement sets the exit status of the function, which is an integer value between 0 and 255. Conventionally, a return status of 0 indicates success, while non-zero values indicate failure or different error codes.
However, if you want to return more complex values (such as a string or a number greater than 255), you need to use echo inside the function and capture that output. Here’s an example:
my_function() {
# Function body
# ...
echo 42
}
In this example, the my_function function returns the value 42 when called.
Returning Multiple Values from a Bash Function
Bash functions can also return multiple values by printing them with echo and separating the values with spaces or newlines. You can then capture the output into a variable and process the values. Here’s an example:
my_function() {
# Function body
# Output multiple values separated by space
echo "value1 value2 value3"
}
To capture multiple values from a function and store them in separate variables, you can use the following syntax:
result=$(my_function)
# Split the result into separate variables
read var1 var2 var3 <<< "$result"
echo "Value 1: $var1"
echo "Value 2: $var2"
echo "Value 3: $var3"
In this example, the function my_function outputs three values separated by spaces. These values are captured into the result variable, then split into individual variables using the read command.
Calling a Bash Function and Getting Return Values
Now that we understand how to define a function and return values from it, let's focus on calling a bash function and retrieving its return values.
To call a function in bash, you simply need to write its name followed by parentheses ()
. For example, if you have a function called my_function, you can call it like this:
my_function
However, to capture the return value of a function, you need to assign it to a variable. Bash provides the $? variable, which holds the return status of the last executed command or function. You can capture the return value of a function into a variable using this syntax:
variable_name=$(my_function)
In the above example, the output of the my_function function is assigned to the variable variable_name. You can then use this variable in your script for further processing or display its value.
Here's a complete example that demonstrates calling a function and retrieving its output:
#!/bin/bash
my_function() {
# Function body
# ...
echo 42
}
# Call the function and capture its return value
result=$(my_function)
# Print the return value
echo "Return value: $result"
In this script, the my_function function outputs the value 42. The output is captured into the result variable, and then it is printed using the echo command.
Conclusion
Bash scripting provides a powerful mechanism for defining functions and calling them within your scripts. By using the return statement, you can return values from functions and capture them into variables when calling the functions. This allows you to create reusable and modular code, enhancing the flexibility and efficiency of your bash scripts.
2 Comments
With all due respect, have you considered a different profession? One you may have some amount of competence in? If not, would you agree some amount of testing should be required prior to publication? A suggestion made with, again, all due respect. All that’s due being none at all, in this case.
‘In this script, the my_function function returns the value 42. The return value is captured into the result variable, and then it is printed…’
You sure about that?
Hi Ed,
Thank you for pointing out the mistake. You’re right — I made an error in the explanation. In Bash, functions can’t return values with return; they can only return an exit status. To capture a value, you need to use echo and store it in a variable. I’ll correct this in the article.
I appreciate your feedback and will make sure to improve in the future. Thanks again for taking the time to help us get better.
Best regards,
Rahul