Fibonacci series -going into infinite loop

Hello, I am a beginner to shell programming. Coded the following for Fibonacci series.

#!/bin/bash
fib()  {

i=0
j=1
arr[0]=0
arr[1]=1

echo "enter the limit:"
read n

while [ $i -lt $n ]
do
    fo= expr $j - 1
    f1=$j
    f2= expr $j + 1
    arr[$f2]= expr ${arr[$f1]} + ${arr[$f0]}
    echo ${arr[$f2]}
    
    i= expr $i + 1
    j= expr $j + 1
    
done
}

fib

The program is going into infinite loop. From debugging I understood that updated i,j values are not being carried to next iteration of while loop. Why is it so? Can someone please help.

Hi, you must use backquote when you execute commande from variable affectation:

i=`expr $i + 1`

or

i=$(expr $i + 1)

or under bash, you needn't use expr, just:

((i++))

Please use code tags as required by forum rules!

You need to use command substitution for all your expr assignments, e.g.

i=$(expr $i + 1)

Thank you disedorgue & RudiC. The changes worked. :wink: