A strange error in Fibonacci recursive function in bash: error in the output












0















I'm trying to write a recursive bash function for Fibonacci sequence. So far I have a code like below:



#!/bin/bash
Fibo() {
case $1 in
0) echo 0;;
1) echo 1;;
*) echo $[$[$Fibo $[$1-1]]+$[$Fibo $[$1-2]]] ;;
esac
}

for (( i=0; i<=$1; i++ )); do
Fibo $i
done


Here's a sample output:



0
1
1
3
5
7
9
11


Of course, it should be 0 1 1 2 3 5 8 13 ...



I tried to write it down in the paper and in my opinion it should work. My code understanding:



Fibo 0 -> = 0
Fibo 1 -> = 1
Fibo 2 -> Fibo 1 + Fibo 0 = 1 + 0 = 1
Fibo 3 -> Fibo 2 + Fibo 1 = 1 + 1 = 2
Fibo 4 -> Fibo 3 + Fibo 2 = 2 + 1 = 3
Fibo 5 -> Fibo 4 + Fibo 3 = 3 + 2 = 5
etc...


What the code "is doing":



Fibo 0 -> = 0
Fibo 1 -> = 1
Fibo 2 -> = 1 + 0 = 1
Fibo 3 -> = 3 (??)
Fibo 4 -> = 5 (this is probably 3+1+1)
Fibo 5 -> = 7 (this is probably once again 5+1+1)
Fibo 6 -> = 9 (once again 7+1+1)


Can you please help me find out where the error lies?
I know there's been many Fibonacci-alike threads already (and I tried to follow the advice given) but I did not find any answer to my problem.



Regards, B










share|improve this question



























    0















    I'm trying to write a recursive bash function for Fibonacci sequence. So far I have a code like below:



    #!/bin/bash
    Fibo() {
    case $1 in
    0) echo 0;;
    1) echo 1;;
    *) echo $[$[$Fibo $[$1-1]]+$[$Fibo $[$1-2]]] ;;
    esac
    }

    for (( i=0; i<=$1; i++ )); do
    Fibo $i
    done


    Here's a sample output:



    0
    1
    1
    3
    5
    7
    9
    11


    Of course, it should be 0 1 1 2 3 5 8 13 ...



    I tried to write it down in the paper and in my opinion it should work. My code understanding:



    Fibo 0 -> = 0
    Fibo 1 -> = 1
    Fibo 2 -> Fibo 1 + Fibo 0 = 1 + 0 = 1
    Fibo 3 -> Fibo 2 + Fibo 1 = 1 + 1 = 2
    Fibo 4 -> Fibo 3 + Fibo 2 = 2 + 1 = 3
    Fibo 5 -> Fibo 4 + Fibo 3 = 3 + 2 = 5
    etc...


    What the code "is doing":



    Fibo 0 -> = 0
    Fibo 1 -> = 1
    Fibo 2 -> = 1 + 0 = 1
    Fibo 3 -> = 3 (??)
    Fibo 4 -> = 5 (this is probably 3+1+1)
    Fibo 5 -> = 7 (this is probably once again 5+1+1)
    Fibo 6 -> = 9 (once again 7+1+1)


    Can you please help me find out where the error lies?
    I know there's been many Fibonacci-alike threads already (and I tried to follow the advice given) but I did not find any answer to my problem.



    Regards, B










    share|improve this question

























      0












      0








      0








      I'm trying to write a recursive bash function for Fibonacci sequence. So far I have a code like below:



      #!/bin/bash
      Fibo() {
      case $1 in
      0) echo 0;;
      1) echo 1;;
      *) echo $[$[$Fibo $[$1-1]]+$[$Fibo $[$1-2]]] ;;
      esac
      }

      for (( i=0; i<=$1; i++ )); do
      Fibo $i
      done


      Here's a sample output:



      0
      1
      1
      3
      5
      7
      9
      11


      Of course, it should be 0 1 1 2 3 5 8 13 ...



      I tried to write it down in the paper and in my opinion it should work. My code understanding:



      Fibo 0 -> = 0
      Fibo 1 -> = 1
      Fibo 2 -> Fibo 1 + Fibo 0 = 1 + 0 = 1
      Fibo 3 -> Fibo 2 + Fibo 1 = 1 + 1 = 2
      Fibo 4 -> Fibo 3 + Fibo 2 = 2 + 1 = 3
      Fibo 5 -> Fibo 4 + Fibo 3 = 3 + 2 = 5
      etc...


      What the code "is doing":



      Fibo 0 -> = 0
      Fibo 1 -> = 1
      Fibo 2 -> = 1 + 0 = 1
      Fibo 3 -> = 3 (??)
      Fibo 4 -> = 5 (this is probably 3+1+1)
      Fibo 5 -> = 7 (this is probably once again 5+1+1)
      Fibo 6 -> = 9 (once again 7+1+1)


      Can you please help me find out where the error lies?
      I know there's been many Fibonacci-alike threads already (and I tried to follow the advice given) but I did not find any answer to my problem.



      Regards, B










      share|improve this question














      I'm trying to write a recursive bash function for Fibonacci sequence. So far I have a code like below:



      #!/bin/bash
      Fibo() {
      case $1 in
      0) echo 0;;
      1) echo 1;;
      *) echo $[$[$Fibo $[$1-1]]+$[$Fibo $[$1-2]]] ;;
      esac
      }

      for (( i=0; i<=$1; i++ )); do
      Fibo $i
      done


      Here's a sample output:



      0
      1
      1
      3
      5
      7
      9
      11


      Of course, it should be 0 1 1 2 3 5 8 13 ...



      I tried to write it down in the paper and in my opinion it should work. My code understanding:



      Fibo 0 -> = 0
      Fibo 1 -> = 1
      Fibo 2 -> Fibo 1 + Fibo 0 = 1 + 0 = 1
      Fibo 3 -> Fibo 2 + Fibo 1 = 1 + 1 = 2
      Fibo 4 -> Fibo 3 + Fibo 2 = 2 + 1 = 3
      Fibo 5 -> Fibo 4 + Fibo 3 = 3 + 2 = 5
      etc...


      What the code "is doing":



      Fibo 0 -> = 0
      Fibo 1 -> = 1
      Fibo 2 -> = 1 + 0 = 1
      Fibo 3 -> = 3 (??)
      Fibo 4 -> = 5 (this is probably 3+1+1)
      Fibo 5 -> = 7 (this is probably once again 5+1+1)
      Fibo 6 -> = 9 (once again 7+1+1)


      Can you please help me find out where the error lies?
      I know there's been many Fibonacci-alike threads already (and I tried to follow the advice given) but I did not find any answer to my problem.



      Regards, B







      bash function unix recursion fibonacci






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 21:35









      BloodyMaryBloodyMary

      547




      547
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Here's an easier way to reproduce your problem:



          #!/bin/bash
          foo() { echo 42; }
          echo $[$foo 1]


          This prints 1.



          This is because $foo is not a valid variable (though there is a function by this name) so it's replaced with nothing. echo $[ 1] naturally prints 1.



          To expand to the result of a function, use $(command substitution):



          #!/bin/bash
          foo() { echo 42; }
          echo $[$(foo 1)]


          This prints 42. Here it is applied to your code:



          #!/bin/bash
          Fibo() {
          case $1 in
          0) echo 0;;
          1) echo 1;;
          *) echo $[$[$(Fibo $[$1-1])]+$[$(Fibo $[$1-2])]] ;;
          esac
          }

          for (( i=0; i<=$1; i++ )); do
          Fibo $i
          done


          Result:



          $ ./foo 10
          0
          1
          1
          2
          3
          5
          8
          13
          21
          34
          55





          share|improve this answer
























          • Thanks for explaining how to get the result of a bash function. I didn't know that I'm doing it in wrong way. I followed your advice and now my code works fine (but it's slightly different than yours :) - "echo $(( $[$(Fibo $(($1-1)))] + $[$(Fibo $(($1-2)))] )) ;;". Thanks again!

            – BloodyMary
            Nov 24 '18 at 22:03











          • @BloodyMary Yours is better since $((..)) is the modern, standard way of doing shell arithmetic evaluation. I only used $[..] because your original example did and cringed a little the whole time.

            – that other guy
            Nov 24 '18 at 22:20











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462570%2fa-strange-error-in-fibonacci-recursive-function-in-bash-error-in-the-output%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Here's an easier way to reproduce your problem:



          #!/bin/bash
          foo() { echo 42; }
          echo $[$foo 1]


          This prints 1.



          This is because $foo is not a valid variable (though there is a function by this name) so it's replaced with nothing. echo $[ 1] naturally prints 1.



          To expand to the result of a function, use $(command substitution):



          #!/bin/bash
          foo() { echo 42; }
          echo $[$(foo 1)]


          This prints 42. Here it is applied to your code:



          #!/bin/bash
          Fibo() {
          case $1 in
          0) echo 0;;
          1) echo 1;;
          *) echo $[$[$(Fibo $[$1-1])]+$[$(Fibo $[$1-2])]] ;;
          esac
          }

          for (( i=0; i<=$1; i++ )); do
          Fibo $i
          done


          Result:



          $ ./foo 10
          0
          1
          1
          2
          3
          5
          8
          13
          21
          34
          55





          share|improve this answer
























          • Thanks for explaining how to get the result of a bash function. I didn't know that I'm doing it in wrong way. I followed your advice and now my code works fine (but it's slightly different than yours :) - "echo $(( $[$(Fibo $(($1-1)))] + $[$(Fibo $(($1-2)))] )) ;;". Thanks again!

            – BloodyMary
            Nov 24 '18 at 22:03











          • @BloodyMary Yours is better since $((..)) is the modern, standard way of doing shell arithmetic evaluation. I only used $[..] because your original example did and cringed a little the whole time.

            – that other guy
            Nov 24 '18 at 22:20
















          2














          Here's an easier way to reproduce your problem:



          #!/bin/bash
          foo() { echo 42; }
          echo $[$foo 1]


          This prints 1.



          This is because $foo is not a valid variable (though there is a function by this name) so it's replaced with nothing. echo $[ 1] naturally prints 1.



          To expand to the result of a function, use $(command substitution):



          #!/bin/bash
          foo() { echo 42; }
          echo $[$(foo 1)]


          This prints 42. Here it is applied to your code:



          #!/bin/bash
          Fibo() {
          case $1 in
          0) echo 0;;
          1) echo 1;;
          *) echo $[$[$(Fibo $[$1-1])]+$[$(Fibo $[$1-2])]] ;;
          esac
          }

          for (( i=0; i<=$1; i++ )); do
          Fibo $i
          done


          Result:



          $ ./foo 10
          0
          1
          1
          2
          3
          5
          8
          13
          21
          34
          55





          share|improve this answer
























          • Thanks for explaining how to get the result of a bash function. I didn't know that I'm doing it in wrong way. I followed your advice and now my code works fine (but it's slightly different than yours :) - "echo $(( $[$(Fibo $(($1-1)))] + $[$(Fibo $(($1-2)))] )) ;;". Thanks again!

            – BloodyMary
            Nov 24 '18 at 22:03











          • @BloodyMary Yours is better since $((..)) is the modern, standard way of doing shell arithmetic evaluation. I only used $[..] because your original example did and cringed a little the whole time.

            – that other guy
            Nov 24 '18 at 22:20














          2












          2








          2







          Here's an easier way to reproduce your problem:



          #!/bin/bash
          foo() { echo 42; }
          echo $[$foo 1]


          This prints 1.



          This is because $foo is not a valid variable (though there is a function by this name) so it's replaced with nothing. echo $[ 1] naturally prints 1.



          To expand to the result of a function, use $(command substitution):



          #!/bin/bash
          foo() { echo 42; }
          echo $[$(foo 1)]


          This prints 42. Here it is applied to your code:



          #!/bin/bash
          Fibo() {
          case $1 in
          0) echo 0;;
          1) echo 1;;
          *) echo $[$[$(Fibo $[$1-1])]+$[$(Fibo $[$1-2])]] ;;
          esac
          }

          for (( i=0; i<=$1; i++ )); do
          Fibo $i
          done


          Result:



          $ ./foo 10
          0
          1
          1
          2
          3
          5
          8
          13
          21
          34
          55





          share|improve this answer













          Here's an easier way to reproduce your problem:



          #!/bin/bash
          foo() { echo 42; }
          echo $[$foo 1]


          This prints 1.



          This is because $foo is not a valid variable (though there is a function by this name) so it's replaced with nothing. echo $[ 1] naturally prints 1.



          To expand to the result of a function, use $(command substitution):



          #!/bin/bash
          foo() { echo 42; }
          echo $[$(foo 1)]


          This prints 42. Here it is applied to your code:



          #!/bin/bash
          Fibo() {
          case $1 in
          0) echo 0;;
          1) echo 1;;
          *) echo $[$[$(Fibo $[$1-1])]+$[$(Fibo $[$1-2])]] ;;
          esac
          }

          for (( i=0; i<=$1; i++ )); do
          Fibo $i
          done


          Result:



          $ ./foo 10
          0
          1
          1
          2
          3
          5
          8
          13
          21
          34
          55






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 21:43









          that other guythat other guy

          74.1k885124




          74.1k885124













          • Thanks for explaining how to get the result of a bash function. I didn't know that I'm doing it in wrong way. I followed your advice and now my code works fine (but it's slightly different than yours :) - "echo $(( $[$(Fibo $(($1-1)))] + $[$(Fibo $(($1-2)))] )) ;;". Thanks again!

            – BloodyMary
            Nov 24 '18 at 22:03











          • @BloodyMary Yours is better since $((..)) is the modern, standard way of doing shell arithmetic evaluation. I only used $[..] because your original example did and cringed a little the whole time.

            – that other guy
            Nov 24 '18 at 22:20



















          • Thanks for explaining how to get the result of a bash function. I didn't know that I'm doing it in wrong way. I followed your advice and now my code works fine (but it's slightly different than yours :) - "echo $(( $[$(Fibo $(($1-1)))] + $[$(Fibo $(($1-2)))] )) ;;". Thanks again!

            – BloodyMary
            Nov 24 '18 at 22:03











          • @BloodyMary Yours is better since $((..)) is the modern, standard way of doing shell arithmetic evaluation. I only used $[..] because your original example did and cringed a little the whole time.

            – that other guy
            Nov 24 '18 at 22:20

















          Thanks for explaining how to get the result of a bash function. I didn't know that I'm doing it in wrong way. I followed your advice and now my code works fine (but it's slightly different than yours :) - "echo $(( $[$(Fibo $(($1-1)))] + $[$(Fibo $(($1-2)))] )) ;;". Thanks again!

          – BloodyMary
          Nov 24 '18 at 22:03





          Thanks for explaining how to get the result of a bash function. I didn't know that I'm doing it in wrong way. I followed your advice and now my code works fine (but it's slightly different than yours :) - "echo $(( $[$(Fibo $(($1-1)))] + $[$(Fibo $(($1-2)))] )) ;;". Thanks again!

          – BloodyMary
          Nov 24 '18 at 22:03













          @BloodyMary Yours is better since $((..)) is the modern, standard way of doing shell arithmetic evaluation. I only used $[..] because your original example did and cringed a little the whole time.

          – that other guy
          Nov 24 '18 at 22:20





          @BloodyMary Yours is better since $((..)) is the modern, standard way of doing shell arithmetic evaluation. I only used $[..] because your original example did and cringed a little the whole time.

          – that other guy
          Nov 24 '18 at 22:20




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462570%2fa-strange-error-in-fibonacci-recursive-function-in-bash-error-in-the-output%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen