Return Values from Bash Script

Bash - Return value from subscript to parent script

I am assuming these scripts are running in two different processes, i.e. you are not "sourcing" one of them.

It depends on what you want to return. If you wish only to return an exit code between 0 and 255 then:

# Child (for example: 'child_script')
exit 42
# Parent
child_script
retn_code=$?

If you wish to return a text string, then you will have to do that through stdout (or a file). There are several ways of capturing that, the simplest is:

# Child (for example: 'child_script')
echo "some text value"
# Parent
retn_value=$(child_script)

return values from bash script

Use command substitution to capture the output of echo, and use arithmetic expression to count the result:

script_a.bash:

echo $(( $1 * 5 ))

script_b.bash

a_value=$( script_a.bash 3 )

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.

How to return a string value from a Bash function

There is no better way I know of. Bash knows only status codes (integers) and strings written to the stdout.

Returning value from called function in a shell script

A Bash function can't return a string directly like you want it to. You can do three things:

  1. Echo a string
  2. Return an exit status, which is a number, not a string
  3. Share a variable

This is also true for some other shells.

Here's how to do each of those options:

1. Echo strings

lockdir="somedir"
testlock(){
retval=""
if mkdir "$lockdir"
then # Directory did not exist, but it was created successfully
echo >&2 "successfully acquired lock: $lockdir"
retval="true"
else
echo >&2 "cannot acquire lock, giving up on $lockdir"
retval="false"
fi
echo "$retval"
}

retval=$( testlock )
if [ "$retval" == "true" ]
then
echo "directory not created"
else
echo "directory already created"
fi

2. Return exit status

lockdir="somedir"
testlock(){
if mkdir "$lockdir"
then # Directory did not exist, but was created successfully
echo >&2 "successfully acquired lock: $lockdir"
retval=0
else
echo >&2 "cannot acquire lock, giving up on $lockdir"
retval=1
fi
return "$retval"
}

testlock
retval=$?
if [ "$retval" == 0 ]
then
echo "directory not created"
else
echo "directory already created"
fi

3. Share variable

lockdir="somedir"
retval=-1
testlock(){
if mkdir "$lockdir"
then # Directory did not exist, but it was created successfully
echo >&2 "successfully acquired lock: $lockdir"
retval=0
else
echo >&2 "cannot acquire lock, giving up on $lockdir"
retval=1
fi
}

testlock
if [ "$retval" == 0 ]
then
echo "directory not created"
else
echo "directory already created"
fi

Bash get return value of a command and exit with this value

I think your problem is that typeset itself creates a return value of 0. Try

gosu user /var/www/bin/phpunit -c app
ret_code=$?
return $ret_code

Bash how to return value from subscript to main script?

One approach would be to use another file to store the result.

  1. In your main.sh, come up with a temp file name.
    Supply this temp file name to menu.sh

  2. Add some handling in menu.sh to ensure that this file is supplied before going on.
    Change the result of menu.sh to write to the temp file

  3. Back in main.sh, read the results from the temp file.
    Then get rid of the file

So, here is main.sh

RESULTS_FILE=results.`date +%s`.txt
./menu.sh $RESULTS_FILE
RESULTS=`cat $RESULTS_FILE`
echo "Got results:"
echo $RESULTS
rm $RESULTS_FILE

And then here is how you'd modify menu.sh

At the top

if [ "$1" == "" ] ; then 
exit "You need to supply a temp file path"
fi
RESULTS_FILE=$1

At the bottom of menu.sh, change the last line to write to the file:

echo "Selected item $cur: ${menu[$cur]}" > $RESULTS_FILE



Related Topics



Leave a reply



Submit