Fibonacci in Bash (original) (raw)

Sample Programs in Every Language

A Collection of Code Snippets in as Many Programming Languages as Possible

This project is maintained by TheRenegadeCoder

  1. Home
  2. Programming Projects in Every Language
  3. Fibonacci
  4. Fibonacci in Bash

Published on 26 October 2018 (Updated: 07 April 2019)

Fibonacci in Bash

Welcome to the Fibonacci in Bash page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#!/bin/bash

count=$1

[[ <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>t</mi><mo>=</mo><msup><mtext> </mtext><mo stretchy="false">[</mo></msup><mn>0</mn><mo>−</mo><mn>9</mn><mo stretchy="false">]</mo><mo>+</mo></mrow><annotation encoding="application/x-tex">count =~ ^[0-9]+</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.9713em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mspace nobreak"> </span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.888em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mopen mtight">[</span></span></span></span></span></span></span></span><span class="mord">0</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">9</span><span class="mclose">]</span><span class="mord">+</span></span></span></span> ]] || { echo "Usage: please input the count of fibonacci numbers to output"; exit 1; }

n=1
n_minus_1=1
[[ $count < 1 ]] && exit 0

echo "1: 1"
for i in <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>s</mi><mi>e</mi><mi>q</mi><mn>2</mn></mrow><annotation encoding="application/x-tex">(seq 2 </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">se</span><span class="mord mathnormal" style="margin-right:0.03588em;">q</span><span class="mord">2</span></span></span></span>count); do
    echo "$i: $n"
    tmp=$n
    n=$[$n+$n_minus_1]
    n_minus_1=$tmp
done

Fibonacci in Bash was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.