7

I know how to return an exit code, but I would like to return the result of an operation done in a shell script function, so I can eventually use it in another script or function.

Something like

var1=$(myfunction)

function2 var1

Where myfunction could be something like A+B=C

I looked into "return", but it will return a code, not a value.

I am looking into various sites that show how to write functions, but I don't see how you actually return values.

In C++ you would use return "variable name", but shell script won't allow this. It says that the variable do not exist (which is logical, it is a variable created in a function, so when the function is released, that memory space assigned to it is gone). Can't use global variables since the function may be in one script and the calling function that needs the return value, may be in a different one.

3 Answers 3

10

myfunction could be something like A+B=C

Just echo the result:

$ myfunction() { echo $(($1+$2)); }

The above myfunction adds two numbers and echoes the result.

The return value can then be captured just as you had it:

$ var=$(myfunction 12 5)
$ echo $var
17

The construct var=$(myfunction) captures the standard out from myfunction and saves it in var. Thus, when you want to return something from myfunction, just send it to standard, like we did with echo in the example above.

In cases where you want the return value to be carefully formatted, you should consider using printf in place of echo.

More: How to return multiple values

Let's define a function that produces two outputs:

$ f() { echo "output1" ; echo "output2" ; }
$ f
output1
output2

If you want to get those values back separately, the most reliable method is to use bash's arrays:

$ a=($(f))

The above executes f, via $(f) and saves the results in an array called a. We can see what is in a by using declare -p:

$ declare -p a
declare -a a='([0]="output1" [1]="output2")'
0
6

I use the same sorta thing for returning values from other scripts to my main script like the title suggests.

At the end of the 2nd script, I echo the variable I want to return to the main script:

#!/bin/bash
# This is the Second Script.

# Store the variables passed from the main script:
VAR1_FROM_MAIN_SCRIPT=$1
VAR2_FROM_MAIN_SCRIPT=$2

# Add the 2 variables and store as another variable to return:
RETURN_THIS=$(($VAR1_FROM_MAIN_SCRIPT + VAR2_FROM_MAIN_SCRIPT)) 

# This is the variable you are sending back to the main script:
echo "$RETURN_THIS"    #<---- This won't print to screen!!!

Then in the main script I pass in a couple variables to, and execute, the 2nd script like this:

#!/bin/bash
# This is the Main Script.

PASS_VAR1_TO_SCRIPT=1
PASS_VAR2_TO_SCRIPT=2

# Call the second script and store it's results in this variable:
RETURN_VARIABLE=$(./secondScriptName "$PASS_VAR1_TO_SCRIPT" "$PASS_VAR2_TO_SCRIPT")

# Display the returned variable from the second script:
echo $RETURN_VARIABLE    #<---- Will display 3

The reason the echo in the second script won't print to screen, is because it's running that second script in a subshell from the RETURN_VARIABLE... I know my explanation of the subshell sucks, but that's besides the point...
Also, I know you can source the other script, but this might help others.

2

In shell scripting you don't return a value but just echo (print) it and caller would capture the output of your script/function to grab the returned value.

Example:

dateval=$(date)
echo $dateval
Wed Apr 23 18:35:45 EDT 2014

Instead of date you can place your function or your shell script.

1
  • 1
    Thanks, and what if you have multiple echo? The function is logging data, so I use echo for that purpose too. Can I differentiate?
    – user393267
    Commented Apr 24, 2014 at 1:50