user784637
user784637

Reputation: 16092

How to properly execute this while loop in bash?

#!/bin/bash

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

I keep getting the error

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

When running ./process.sh 5

Upvotes: 2

Views: 108

Answers (2)

Andrew White
Andrew White

Reputation: 53496

The $ means "value of" so $f gets evaluated to the string literal 1. So...

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

should be

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

Upvotes: 5

zzk
zzk

Reputation: 1387

in the loop, it should be

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

Upvotes: 1

Related Questions