如何在bash中正确执行此循环?

时间:2013-03-10 00:34:18

标签: linux bash

#!/bin/bash

f=1
c=$1
while [[ $c != 0 ]]
do
        $f=$(($c*$f))
        $c=$(($c-1))
done
echo $c

我一直收到错误

./process.sh: line 8: 1=0: command not found
./process.sh: line 7: 5=5: command not found

运行./process.sh 5

2 个答案:

答案 0 :(得分:5)

$表示“值”,因此$f会对字符串文字1进行评估。所以......

    $f=$(($c*$f))
    $c=$(($c-1))

应该是

    f=$(($c*$f))
    c=$(($c-1))

答案 1 :(得分:1)

在循环中,它应该是

f=$(($c*$f))
c=$(($c-1))