Factorial in Unix shell
Example for versions
Bash 3.0,
Bash 4.0.35
This example uses recursive factorial definition.
factorial ()
{
local num=$1;
if [ $num = 0 ]; then
echo 1
return ;
fi;
echo $(( $num * $(factorial $(( $num - 1 )) ) ))
}
for ((n = 0; n <= 16; n++))
do
echo "$n! = " $(factorial $(($n)))
done
Comments
]]>blog comments powered by Disqus
]]>