In function_two, I need to get both the output from echo and the return value from function_one
#!/bin/bash
function_one() {
echo "okay then"
return 2
}
function_two() {
local a_function="function_one"
local string=`${a_function}`
echo $string # echos "okay then"
echo "$?" #echos 0 - how do we get the returned value of 2?
}
function_two
When trying echo "$?" I get 0 instead of 2
Update
As Ipor Sircer pointed out, $? above is giving the exit code of the previous command echo $string
So instead I grab the exit code immediately after. And as choroba mentioned, the localization and assignment of the variable needed to be separated.
Here is the working script:
#!/bin/bash
function_one() {
echo "okay then"
return 2
}
function_two() {
local a_function="function_one"
local string
string=`${a_function}`
local exitcode=$?
echo "string: $string" # okay then
echo "exitcode: $exitcode" # 2
}
function_two
echo $stringcommand has run successfully, so the exitcode is 0.