I want print echo in function and return value. It's not working:
function fun1() {
echo "Start function"
return "2"
}
echo $(( $(fun1) + 3 ))
I can only print echo:
function fun1() {
echo "Start function"
}
fun1
Or I can only return value:
function fun1() {
echo "2" # returning value by echo
}
echo $(( $(fun1) + 3 ))
But I can't do both.
Best Answer
Well, depending on what you wish, there are several solutions:
Print the message to
stderr
and the value you wish to take instdout
.Print the message normally to
stdout
and use the actual return value with$?
.Note that the return value will always be a value from
0
-255
(Thanks Gordon Davisson).Simply use the global variable.