variable with a variable substring in bash [duplicate]

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











up vote
0
down vote

favorite













This question already has an answer here:



  • How to do indirect variable evaluation

    5 answers



In bash, I have defined



chg_Li=3
chg_Na=9


Now I want to call $chg_$i where i is in a for loop, looping over Li and Na.



What is the correct syntax to call $chg_$i?










share|improve this question















marked as duplicate by don_crissti, αғsнιη, Jeff Schaller, peterh, jasonwryan Oct 3 '17 at 23:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    0
    down vote

    favorite













    This question already has an answer here:



    • How to do indirect variable evaluation

      5 answers



    In bash, I have defined



    chg_Li=3
    chg_Na=9


    Now I want to call $chg_$i where i is in a for loop, looping over Li and Na.



    What is the correct syntax to call $chg_$i?










    share|improve this question















    marked as duplicate by don_crissti, αғsнιη, Jeff Schaller, peterh, jasonwryan Oct 3 '17 at 23:05


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:



      • How to do indirect variable evaluation

        5 answers



      In bash, I have defined



      chg_Li=3
      chg_Na=9


      Now I want to call $chg_$i where i is in a for loop, looping over Li and Na.



      What is the correct syntax to call $chg_$i?










      share|improve this question
















      This question already has an answer here:



      • How to do indirect variable evaluation

        5 answers



      In bash, I have defined



      chg_Li=3
      chg_Na=9


      Now I want to call $chg_$i where i is in a for loop, looping over Li and Na.



      What is the correct syntax to call $chg_$i?





      This question already has an answer here:



      • How to do indirect variable evaluation

        5 answers







      bash variable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 3 '17 at 22:10









      αғsнιη

      15.6k92563




      15.6k92563










      asked Oct 3 '17 at 22:05









      Lei Zhang

      1




      1




      marked as duplicate by don_crissti, αғsнιη, Jeff Schaller, peterh, jasonwryan Oct 3 '17 at 23:05


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by don_crissti, αғsнιη, Jeff Schaller, peterh, jasonwryan Oct 3 '17 at 23:05


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can use eval or indirection:



          eval echo $chg_$i


          or



          varname=$chg_$i
          echo $!varname





          share|improve this answer
















          • 1




            Preferably indirection. When using eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.
            – DopeGhoti
            Oct 3 '17 at 22:34

















          up vote
          0
          down vote













          As was mentioned, you can use indirection for this:



          $ chg_Li=3
          $ chg_Na=9
          $ post_fix=(Li Na)
          $ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
          3
          9





          share|improve this answer



























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            You can use eval or indirection:



            eval echo $chg_$i


            or



            varname=$chg_$i
            echo $!varname





            share|improve this answer
















            • 1




              Preferably indirection. When using eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.
              – DopeGhoti
              Oct 3 '17 at 22:34














            up vote
            1
            down vote













            You can use eval or indirection:



            eval echo $chg_$i


            or



            varname=$chg_$i
            echo $!varname





            share|improve this answer
















            • 1




              Preferably indirection. When using eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.
              – DopeGhoti
              Oct 3 '17 at 22:34












            up vote
            1
            down vote










            up vote
            1
            down vote









            You can use eval or indirection:



            eval echo $chg_$i


            or



            varname=$chg_$i
            echo $!varname





            share|improve this answer












            You can use eval or indirection:



            eval echo $chg_$i


            or



            varname=$chg_$i
            echo $!varname






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 3 '17 at 22:07









            Hauke Laging

            53.7k1282130




            53.7k1282130







            • 1




              Preferably indirection. When using eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.
              – DopeGhoti
              Oct 3 '17 at 22:34












            • 1




              Preferably indirection. When using eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.
              – DopeGhoti
              Oct 3 '17 at 22:34







            1




            1




            Preferably indirection. When using eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.
            – DopeGhoti
            Oct 3 '17 at 22:34




            Preferably indirection. When using eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.
            – DopeGhoti
            Oct 3 '17 at 22:34












            up vote
            0
            down vote













            As was mentioned, you can use indirection for this:



            $ chg_Li=3
            $ chg_Na=9
            $ post_fix=(Li Na)
            $ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
            3
            9





            share|improve this answer
























              up vote
              0
              down vote













              As was mentioned, you can use indirection for this:



              $ chg_Li=3
              $ chg_Na=9
              $ post_fix=(Li Na)
              $ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
              3
              9





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                As was mentioned, you can use indirection for this:



                $ chg_Li=3
                $ chg_Na=9
                $ post_fix=(Li Na)
                $ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
                3
                9





                share|improve this answer












                As was mentioned, you can use indirection for this:



                $ chg_Li=3
                $ chg_Na=9
                $ post_fix=(Li Na)
                $ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
                3
                9






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 3 '17 at 22:50









                FloHe

                63728




                63728












                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)