Recursive function in bash

后端 未结 6 1661
渐次进展
渐次进展 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:27

    #-----------------factorial ------------------------
    # using eval to avoid process creation
    fac=25
    factorial()
    {
        if [[ $1 -le 1 ]]
        then
            eval $2=1
        else
    
            factorial $[$1-1] $2
            typeset f2
            eval f2=\$$2
            ((f2=f2*$1))
            eval $2=$f2
    
        fi
    }
    time factorial $fac res 
    echo "factorial =$res"
    

提交回复
热议问题