Reputation: 6522
I'm trying to make a function that will output fibonacci numbers. This is the code:
#!/bin/bash
a1=0;
a2=1;
echo "Vnesi n"
read n
echo $a1
echo $a2
for ((i = 1; i <= $n; i++)) do
a3=$(($a1+$a2))
echo $a3
$a1=$a2
$a2=$a3
done
When I run it, it gets to to line 10 (echo $a3) and then outputs an error:
1
0
1
1
./fib.sh: line 11: 0=1: command not found
./fib.sh: line 12: 1=1: command not found
Basically what I'm trying to do is to pass value from a2 to a1 and value from a3 to a2. What am I doing wrong here?
Upvotes: 0
Views: 37
Reputation: 445
a1=$a2
not $a1
otherwise the left side is evaluated and the value (0 or whatever) is used
Upvotes: 0
Reputation: 531165
Your first variable assignments are correct:
a1=0
a2=1
The second ones incorrectly prefix the left-hand side with a dollar sign:
$a1=$a2 # Should be a1=$a2
$a2=$a3 # Should be a2=$a3
Upvotes: 2