How do you find the factorial of a number in a Bash script?

前端 未结 13 2111
别那么骄傲
别那么骄傲 2021-02-14 03:24

In shell scripting how to find factorial of a number?

13条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-14 03:45

    10! in bash:

    f=1; for k in {1..10}; do f=$[$k * $f] ; done; echo $f
    

    or here in a step by step fashion:

    $ t=$(echo {1..10})
    $ echo $t
    1 2 3 4 5 6 7 8 9 10
    $ t=${t// /*}
    $ echo $t
    1*2*3*4*5*6*7*8*9*10
    $ echo $[$t]
    3628800
    

提交回复
热议问题