Recursive function in bash

后端 未结 6 1658
渐次进展
渐次进展 2020-12-13 09:33

I want to do a function that will return the factorial of a number in bash

Here\'s the current code that doesn\'t work, can anyone tell me what\'s wrong and how to c

6条回答
  •  悲哀的现实
    2020-12-13 10:16

    Another one implementation using echo instead of return

    #!/bin/bash
    
    factorial()
    {
            if [ $1 -le 1 ]
            then
                    echo 1
            else
                    echo $[ $1 * `factorial $[$1-1]` ]
            fi
    }
    echo "factorial $1 = " `factorial $1`
    

提交回复
热议问题