Return value in a Bash function Return value in a Bash function bash bash

Return value in a Bash function


Although Bash has a return statement, the only thing you can specify with it is the function's own exit status (a value between 0 and 255, 0 meaning "success"). So return is not what you want.

You might want to convert your return statement to an echo statement - that way your function output could be captured using $() braces, which seems to be exactly what you want.

Here is an example:

function fun1(){  echo 34}function fun2(){  local res=$(fun1)  echo $res}

Another way to get the return value (if you just want to return an integer 0-255) is $?.

function fun1(){  return 34}function fun2(){  fun1  local res=$?  echo $res}

Also, note that you can use the return value to use Boolean logic - like fun1 || fun2 will only run fun2 if fun1 returns a non-0 value. The default return value is the exit value of the last statement executed within the function.


$(...) captures the text sent to standard output by the command contained within. return does not output to standard output. $? contains the result code of the last command.

fun1 (){  return 34}fun2 (){  fun1  local res=$?  echo $res}


Functions in Bash are not functions like in other languages; they're actually commands. So functions are used as if they were binaries or scripts fetched from your path. From the perspective of your program logic, there shouldn't really be any difference.

Shell commands are connected by pipes (aka streams), and not fundamental or user-defined data types, as in "real" programming languages. There is no such thing like a return value for a command, maybe mostly because there's no real way to declare it. It could occur on the man-page, or the --help output of the command, but both are only human-readable and hence are written to the wind.

When a command wants to get input it reads it from its input stream, or the argument list. In both cases text strings have to be parsed.

When a command wants to return something, it has to echo it to its output stream. Another often practiced way is to store the return value in dedicated, global variables. Writing to the output stream is clearer and more flexible, because it can take also binary data. For example, you can return a BLOB easily:

encrypt() {    gpg -c -o- $1 # Encrypt data in filename to standard output (asks for a passphrase)}encrypt public.dat > private.dat # Write the function result to a file

As others have written in this thread, the caller can also use command substitution $() to capture the output.

Parallely, the function would "return" the exit code of gpg (GnuPG). Think of the exit code as a bonus that other languages don't have, or, depending on your temperament, as a "Schmutzeffekt" of shell functions. This status is, by convention, 0 on success or an integer in the range 1-255 for something else. To make this clear: return (like exit) can only take a value from 0-255, and values other than 0 are not necessarily errors, as is often asserted.

When you don't provide an explicit value with return, the status is taken from the last command in a Bash statement/function/command and so forth. So there is always a status, and return is just an easy way to provide it.