facebook youtube pinterest twitter reddit whatsapp instagram

Returning Values From Functions in (Bash)

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"

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

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.

Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f

  • Sort in Bash Scripting

    We recently just looked at grep in bash scripting, and another commonly used program in bash scripting is the program called sort. Sort is a simple filter that is used to sort lines of text from its