echo works better than return + $?, since consecutive call f $(( a-1 )), f $(( a-2 )) does not set $? correctly.
the result of echo can be captured at the final step.
#!/bin/bash
f(){
if [[ $1 -eq 1 || $1 -eq 2 ]]; then # int differ from string compare
echo 1
else
a=$1
echo $(( $( f $((a-1)) ) + $( f $(( a-2))) )) # $(( )) substitute var/func
fi
}
f $1
v=$(( $(f $1) )) # capture echo into variable
echo $v
No comments:
Post a Comment