Fibonacci in BASH

So here’s the big surprise. Shell scripting! It’s not what most people think of when they think of programming, but systems administrators are still doing lots of scripting work to automate tasks. Shell scripting can tackle some pretty serious and complicated problems, so running our Fibonacci program is no sweat. If you haven’t seen the Fibonacci in C and the Fibonacci in PHP, now might be a good time to review them.

So what are the gotchas for scripting in BASH? What killed me is whitespace! (a=1 is not the same as a = 1). Once you get the hang of it, it’s not a big deal, but if you are used to working in PHP or another language that ignores whitespace, you’ll run through a few syntax errors before figuring it out. Using an editor like Vim that has syntax highlighting will help a lot there.So there is the third version of our Fibonacci project. Coming soon, I’ll take on the popular scripting languages perl, python, and ruby.

3 Responses to “Fibonacci in BASH”

  1. John Says:

    This is great, thank you (bookmarked). I have been (sloooowly) moving over to Linux for LAMP and C / C++ apps. I do however have alot of little jobs that would be better suited to a script, so this is a great starter tutorial.

    Now, if I could figure out how to use cron jobs to call these scripts that would be first class…

    Thanks again…

  2. raiser Says:

    and again…
    this code has a max value by 32Bit
    (6431964919686552569)
    max while-> iterations 921 😀

  3. raiser Says:

    sry.. i’ve see by debugging:

    if [ $sum -lt 0 ] ; then
    echo $sum : $i
    exit
    fi

    the max value is: 7540113804746346429
    i: 89