How many levels of indirection can I apply in Bash?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












In bash, I understand we can have variable indirect expansion via two ways:



  • Using declare: declare -n foo=bar

  • Using the $!.. expansion.

We can combine both:



declare -n foo=SHELL
bar=foo
echo $!bar


gives:



/bin/bash


Is it possible to extend this to more levels?




It's mostly as for writing obfuscated code - some of my friends are challenging each other.










share|improve this question







New contributor




avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    2
    down vote

    favorite












    In bash, I understand we can have variable indirect expansion via two ways:



    • Using declare: declare -n foo=bar

    • Using the $!.. expansion.

    We can combine both:



    declare -n foo=SHELL
    bar=foo
    echo $!bar


    gives:



    /bin/bash


    Is it possible to extend this to more levels?




    It's mostly as for writing obfuscated code - some of my friends are challenging each other.










    share|improve this question







    New contributor




    avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      In bash, I understand we can have variable indirect expansion via two ways:



      • Using declare: declare -n foo=bar

      • Using the $!.. expansion.

      We can combine both:



      declare -n foo=SHELL
      bar=foo
      echo $!bar


      gives:



      /bin/bash


      Is it possible to extend this to more levels?




      It's mostly as for writing obfuscated code - some of my friends are challenging each other.










      share|improve this question







      New contributor




      avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      In bash, I understand we can have variable indirect expansion via two ways:



      • Using declare: declare -n foo=bar

      • Using the $!.. expansion.

      We can combine both:



      declare -n foo=SHELL
      bar=foo
      echo $!bar


      gives:



      /bin/bash


      Is it possible to extend this to more levels?




      It's mostly as for writing obfuscated code - some of my friends are challenging each other.







      bash indirection






      share|improve this question







      New contributor




      avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 21 at 13:30









      avidenat

      1




      1




      New contributor




      avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      avidenat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Actually, there is a further method to do indirections in Bash, at least effectively: If you have a=1 and b=a, you can get the value of a through b like so:



          eval echo $$b


          This can be nested several times:



          $ a=1; b=a; c=b; d=c
          $ eval eval eval echo \\\$\$$$d
          1


          Here are the rules to find the right number of backslashes on each level:



          1. On the innermost level, no backslashes are used.

          2. On any other level, use 2 n + 1 backslashes, where n is the number of backslashes that are used on the next inner level.

          Rationale: Rule 1 is trivial. Rule 2 comes from the fact that you have to apply one backslash more that in the next inner level while you have to escape all those that are not consumed in the current level, i.e. all but one.



          As a consequence, the number of needed backslashes diverges exponentially with increasing number of levels so that nesting reaches its limit rather soon for this method.



          But one has to emphasize here that this limit is of rather academic nature. In practice, where one has to respect needs such like maintainability, one typically does not want to handle more than two or three levels of indirection in one single expression—no matter which method of indirection is used.



          Instead, one can resolve higher levels of indirection by using a loop to iterate something like



          value=$variable
          variable=$!value


          a number of times that is suitable for the given application.






          share|improve this answer





























            up vote
            0
            down vote













            There is a (large) indirection allowed directly in arithmetic expansion for only numbers.



            $ a=123 b=a c=b d=c e=d ; echo $((e))
            123


            you could expand it with declare:



            $ jj=123; for ii in a..fa..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
            123


            But it is not infinite:



            $ jj=123; for ii in a..za..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
            bash: tz: expression recursion level exceeded (error token is "tz")


            Is that enough?






            share|improve this answer




















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              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',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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
              );



              );






              avidenat is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483207%2fhow-many-levels-of-indirection-can-i-apply-in-bash%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              Actually, there is a further method to do indirections in Bash, at least effectively: If you have a=1 and b=a, you can get the value of a through b like so:



              eval echo $$b


              This can be nested several times:



              $ a=1; b=a; c=b; d=c
              $ eval eval eval echo \\\$\$$$d
              1


              Here are the rules to find the right number of backslashes on each level:



              1. On the innermost level, no backslashes are used.

              2. On any other level, use 2 n + 1 backslashes, where n is the number of backslashes that are used on the next inner level.

              Rationale: Rule 1 is trivial. Rule 2 comes from the fact that you have to apply one backslash more that in the next inner level while you have to escape all those that are not consumed in the current level, i.e. all but one.



              As a consequence, the number of needed backslashes diverges exponentially with increasing number of levels so that nesting reaches its limit rather soon for this method.



              But one has to emphasize here that this limit is of rather academic nature. In practice, where one has to respect needs such like maintainability, one typically does not want to handle more than two or three levels of indirection in one single expression—no matter which method of indirection is used.



              Instead, one can resolve higher levels of indirection by using a loop to iterate something like



              value=$variable
              variable=$!value


              a number of times that is suitable for the given application.






              share|improve this answer


























                up vote
                0
                down vote













                Actually, there is a further method to do indirections in Bash, at least effectively: If you have a=1 and b=a, you can get the value of a through b like so:



                eval echo $$b


                This can be nested several times:



                $ a=1; b=a; c=b; d=c
                $ eval eval eval echo \\\$\$$$d
                1


                Here are the rules to find the right number of backslashes on each level:



                1. On the innermost level, no backslashes are used.

                2. On any other level, use 2 n + 1 backslashes, where n is the number of backslashes that are used on the next inner level.

                Rationale: Rule 1 is trivial. Rule 2 comes from the fact that you have to apply one backslash more that in the next inner level while you have to escape all those that are not consumed in the current level, i.e. all but one.



                As a consequence, the number of needed backslashes diverges exponentially with increasing number of levels so that nesting reaches its limit rather soon for this method.



                But one has to emphasize here that this limit is of rather academic nature. In practice, where one has to respect needs such like maintainability, one typically does not want to handle more than two or three levels of indirection in one single expression—no matter which method of indirection is used.



                Instead, one can resolve higher levels of indirection by using a loop to iterate something like



                value=$variable
                variable=$!value


                a number of times that is suitable for the given application.






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Actually, there is a further method to do indirections in Bash, at least effectively: If you have a=1 and b=a, you can get the value of a through b like so:



                  eval echo $$b


                  This can be nested several times:



                  $ a=1; b=a; c=b; d=c
                  $ eval eval eval echo \\\$\$$$d
                  1


                  Here are the rules to find the right number of backslashes on each level:



                  1. On the innermost level, no backslashes are used.

                  2. On any other level, use 2 n + 1 backslashes, where n is the number of backslashes that are used on the next inner level.

                  Rationale: Rule 1 is trivial. Rule 2 comes from the fact that you have to apply one backslash more that in the next inner level while you have to escape all those that are not consumed in the current level, i.e. all but one.



                  As a consequence, the number of needed backslashes diverges exponentially with increasing number of levels so that nesting reaches its limit rather soon for this method.



                  But one has to emphasize here that this limit is of rather academic nature. In practice, where one has to respect needs such like maintainability, one typically does not want to handle more than two or three levels of indirection in one single expression—no matter which method of indirection is used.



                  Instead, one can resolve higher levels of indirection by using a loop to iterate something like



                  value=$variable
                  variable=$!value


                  a number of times that is suitable for the given application.






                  share|improve this answer














                  Actually, there is a further method to do indirections in Bash, at least effectively: If you have a=1 and b=a, you can get the value of a through b like so:



                  eval echo $$b


                  This can be nested several times:



                  $ a=1; b=a; c=b; d=c
                  $ eval eval eval echo \\\$\$$$d
                  1


                  Here are the rules to find the right number of backslashes on each level:



                  1. On the innermost level, no backslashes are used.

                  2. On any other level, use 2 n + 1 backslashes, where n is the number of backslashes that are used on the next inner level.

                  Rationale: Rule 1 is trivial. Rule 2 comes from the fact that you have to apply one backslash more that in the next inner level while you have to escape all those that are not consumed in the current level, i.e. all but one.



                  As a consequence, the number of needed backslashes diverges exponentially with increasing number of levels so that nesting reaches its limit rather soon for this method.



                  But one has to emphasize here that this limit is of rather academic nature. In practice, where one has to respect needs such like maintainability, one typically does not want to handle more than two or three levels of indirection in one single expression—no matter which method of indirection is used.



                  Instead, one can resolve higher levels of indirection by using a loop to iterate something like



                  value=$variable
                  variable=$!value


                  a number of times that is suitable for the given application.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago

























                  answered 2 days ago









                  Jürgen

                  717




                  717






















                      up vote
                      0
                      down vote













                      There is a (large) indirection allowed directly in arithmetic expansion for only numbers.



                      $ a=123 b=a c=b d=c e=d ; echo $((e))
                      123


                      you could expand it with declare:



                      $ jj=123; for ii in a..fa..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
                      123


                      But it is not infinite:



                      $ jj=123; for ii in a..za..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
                      bash: tz: expression recursion level exceeded (error token is "tz")


                      Is that enough?






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        There is a (large) indirection allowed directly in arithmetic expansion for only numbers.



                        $ a=123 b=a c=b d=c e=d ; echo $((e))
                        123


                        you could expand it with declare:



                        $ jj=123; for ii in a..fa..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
                        123


                        But it is not infinite:



                        $ jj=123; for ii in a..za..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
                        bash: tz: expression recursion level exceeded (error token is "tz")


                        Is that enough?






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          There is a (large) indirection allowed directly in arithmetic expansion for only numbers.



                          $ a=123 b=a c=b d=c e=d ; echo $((e))
                          123


                          you could expand it with declare:



                          $ jj=123; for ii in a..fa..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
                          123


                          But it is not infinite:



                          $ jj=123; for ii in a..za..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
                          bash: tz: expression recursion level exceeded (error token is "tz")


                          Is that enough?






                          share|improve this answer












                          There is a (large) indirection allowed directly in arithmetic expansion for only numbers.



                          $ a=123 b=a c=b d=c e=d ; echo $((e))
                          123


                          you could expand it with declare:



                          $ jj=123; for ii in a..fa..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
                          123


                          But it is not infinite:



                          $ jj=123; for ii in a..za..z; do declare $ii=$jj; jj=$ii; done; echo "$((ii))"
                          bash: tz: expression recursion level exceeded (error token is "tz")


                          Is that enough?







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 days ago









                          Isaac

                          9,70311445




                          9,70311445




















                              avidenat is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              avidenat is a new contributor. Be nice, and check out our Code of Conduct.












                              avidenat is a new contributor. Be nice, and check out our Code of Conduct.











                              avidenat is a new contributor. Be nice, and check out our Code of Conduct.













                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483207%2fhow-many-levels-of-indirection-can-i-apply-in-bash%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

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?