title: A recursive fibonacci function in bash
body: <p>I was perusing the sight for some help with my code and came across a thread from about 4 months ago, however the user's final revision doesn't work when posted into bash, and produces some strange results.  Here is my version, which also produces some strange results: </p>
  
  <pre><code>#!/bin/bash
  fib()
  {
  ind=$1
  
  if (( ind &lt;= 0 ))
   then echo 0
  elif (( ind = 1 ))
   then echo 1
  else
    echo $(( $(fib $((ind - 1)) ) + $(fib $((ind - 2)) ) )) 
  fi
  }
  echo fibbonacci sequence number $1 is $(fib $1)
  </code></pre>
  
  <p>so this block of code will end up <em>always</em> outputting 1.  ./fib.sh 5 outputs 
  fibbonacci sequence number 5 is 1</p>
  
  <p>so I tried to write the code a little closer to what the previous asker had,</p>
  
  <pre><code>#!/bin/bash
  
  fib()
   {
    ind=$1
  
  if (( ind &lt;= 0 ))
   then echo 1
  else
    echo $(( $(fib $((ind - 1)) ) + $(fib $((ind - 2)) ) )) 
  fi
   }
   echo fibbonacci sequence number $1 is $(fib $1)
  </code></pre>
  
  <p>While I don't understand the logic here, it actually starts to output fibonacci numbers,
  but now I get a slightly different problem; ./fib.sh 3 outputs:
  fibbonacci sequence number 3 is 5
  ./fib.sh 5 outputs : 
  fibbonacci sequence number 5 is 13
  Well we know that the 3rd fibonacci number is 1, and the 5th is 3 so what gives? The code seems to skip ahead several fibonacci numbers, and I can't figure out what is logically wrong with my code.</p>