Returning Values From Functions in (Bash)

Last Updated on: September 14, 2020

In the guide function in bash, we dive deep in bash functions and we also pass some information into the functions to carry out a certain operation.

In this guide, I’ll show you how to get information back out of a function. A function can return information to the calling environment in two different ways, the first way is using the output (this just printing information to the screen), and the second way is by way of an exit status, which reports if the code is success or failure, The value must be 0 to indicate success and a different value to indicate failure.

First, let’s talk about the function output, this would help you to get a better understanding of how the return value works, so…

Function output is the combined output of all the programs run within the code block of function, for example, the echo or ls program, if those programs produce an output, then the function produces an output, it is as simple as that.

Here is where things get interesting, typically, the output produced by this program will practically go on to the screen, but you can actually get it to go somewhere else, you’ve probably seen this before (an example is Pipes and Redirection in Bash) not just in conjunction with functions, you can send the output of a function to another program e.g function_sample | wc -l would count the number of lines in the output, and you could also redirect the output of the function to another file.

Another superb idea is to to take the output of that function and store it in a variable using the back quotes e.g output = `sample_func`

Now the next part, every shell command produces an exit status, and functions are no exception, they produce exit statuses even if you don’t explicitly program your function to set one yourself.

The way it works is that if you don’t specify an exit status for your function, the exit status of the last command that your function executes will be used, for example, if your function contains the following:

function user_details {
echo "Current User"
whoami
}

The exit status of the whoami will be the exit status of your function, if you actually want to specify an exit status, you must use the return statement with a number which serves as the exit code, 0 mean success, and anything other than zero means failure.

Note that you can say “return true” or “return false”, you need to specify the exit code.

Let’s take a quick example of a function that uses the return value:

vaid_file ()
{
 [ ! -r $1 ] && return 1
 grep mylog $1 > /dev/null || return 1
 return 0
}

The first thing we checked is if the file is readable or not, if it is not readable it would return 1, which means it is an error or false.

If the file is actually readable, and it contains the word “mylog”, it is a valid file, what this does is use the grep command to find the word “mylog”, and if it doesn’t find that, it will return 1 as above, if it finds the word “mylog” then it is successful, so we can safely return zero.

Note that no output is produced at any time, not even when grep succeeded, I have simply redirected the grep output to the dev/null, which means nothing.

We can use the function in the following way:

if valid_file /logs/mylog.log
then
...

We are using the above code to check if mylog.log is a valid file, if it is then blah blah blah, so we can do some particular operation on the file if it is valid.

There is one thing that we need to keep in mind when using the return statement, and that is it is similar to the exit statement. The difference is we use that exit statement terminates the execution of the entire shell and you’ll be left back at your login shell, while the return statement terminates the execution of the current function, and that’s all, don’t get it twisted.

Whether you use exit or return, they both take an optional parameter, which is a number that represents the exit status that is returned to the calling environment.

Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

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