I am a Java guy, learning how to write a clean bash script. I am writing a bash function that does some calculations. During the run, this function must log something that shows how the internal calculation goes. Then at the end, it returns the computed value.
This way:
calculate() {
local some_varible_1
# doing something here
printf "result: %s\n" "$some_varible_1"
local result
# doing something again
printf "result: %s\n" "$result"
# return value
# the stdout must be cleared somehow
printf "%s" "$result"
}
The function does not need (and does not have) any input param because it works with script level global variables and I would like to avoid using input param only for passing back the return value.
This is how I call this function:
computed_valuee=$(calculate)
Is that possible to reset the stdout somehow before returning the result from the function? What is the best way to write a bash function that has a return value but the function logs info to stdout as well?