I have a simple factorial recursive function defined here. it works okay until num=5 but later on, it starts giving garbage values. I'm reading the num from the user and then feeding it into the recursive function. I'm not able to figure out the issue as I'm new to the arithmetic side of bash.
function fact {
if (( $1==1 ))
then
return 1
else
prev=$(( $1-1 ))
fact $prev
prev_fact=$?
this_fact=$( expr $1 \* $prev_fact )
# this_fact=$(( $1 * $prev_fact )) This also works the same way as above
return $this_fact
fi
}
echo -n "Enter the number: "
read num
fact $num
val=$?
echo "Factorial of "$num" is "$val""