I would like to print the factorial of number read from stdin
but I cannot do it this way (this prints empty line):
#!/bin/bash
factorial()
{
if [ $1 -le 1 ]
then
return 1
else
factorial $[$1-1]
return $[$1*$?]
fi
}
read num
ret="$(factorial $num)"
echo "${ret}"
This way worked but I feel it's a bit worse (as I cannot save the variable for later):
factorial $num
echo $?
Why is the first method not working? (link to highly upvoted answer on SO that explains it)