Is it possible to build variable names from other variables in bash? [duplicate]












28
















This question already has an answer here:




  • Dynamic variable names in Bash

    11 answers




I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:



for I in 1 2 3 4
echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.


Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I Is this possible in Bash?










share|improve this question













marked as duplicate by tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Aug 24 '16 at 8:56


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.























    28
















    This question already has an answer here:




    • Dynamic variable names in Bash

      11 answers




    I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:



    for I in 1 2 3 4
    echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.


    Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I Is this possible in Bash?










    share|improve this question













    marked as duplicate by tripleee bash
    Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Aug 24 '16 at 8:56


    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.





















      28












      28








      28


      6







      This question already has an answer here:




      • Dynamic variable names in Bash

        11 answers




      I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:



      for I in 1 2 3 4
      echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.


      Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I Is this possible in Bash?










      share|improve this question















      This question already has an answer here:




      • Dynamic variable names in Bash

        11 answers




      I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:



      for I in 1 2 3 4
      echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.


      Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I Is this possible in Bash?





      This question already has an answer here:




      • Dynamic variable names in Bash

        11 answers








      bash variables






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 18 '10 at 21:20









      StephenStephen

      4,32122851




      4,32122851




      marked as duplicate by tripleee bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Aug 24 '16 at 8:56


      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 tripleee bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Aug 24 '16 at 8:56


      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.


























          7 Answers
          7






          active

          oldest

          votes


















          34














          Yes, but don't do that. Use an array instead.



          If you still insist on doing it that way...



          $ foo1=123
          $ bar=foo1
          $ echo "${!bar}"
          123





          share|improve this answer



















          • 5





            +1 for but don't do that. Use an array instead.

            – Nifle
            Oct 18 '10 at 21:25











          • Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.

            – Stephen
            Oct 18 '10 at 21:44











          • it doesnt work for kshell

            – g4ur4v
            Jul 23 '14 at 10:28



















          10














          for I in 1 2 3 4 5; do
          TMP="VAR$I"
          echo ${!TMP}
          done


          I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.






          share|improve this answer

































            4














            You should think about using bash arrays for this sort of work:



            pax> set arr=(9 8 7 6)
            pax> set idx=2
            pax> echo ${arr[!idx]}
            7





            share|improve this answer































              4














              For the case where you don't want to refactor your variables into arrays...



              One way...



              $ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done


              Another way...



              $ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done


              I haven't found a simpler way that works. For example, this does not work...



              $ for I in 1 2 3 4; do echo ${!VAR$I} ; done
              bash: ${!VAR$I}: bad substitution





              share|improve this answer



















              • 1





                Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.

                – nobar
                Apr 2 '14 at 16:16



















              3














              for I in {1..5}; do
              echo $((VAR$I))
              done





              share|improve this answer































                1














                Yes. See Advanced Bash-Scripting Guide






                share|improve this answer
























                • It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!

                  – Charles Roberto Canato
                  Dec 6 '17 at 19:16



















                0














                This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays






                share|improve this answer






























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  34














                  Yes, but don't do that. Use an array instead.



                  If you still insist on doing it that way...



                  $ foo1=123
                  $ bar=foo1
                  $ echo "${!bar}"
                  123





                  share|improve this answer



















                  • 5





                    +1 for but don't do that. Use an array instead.

                    – Nifle
                    Oct 18 '10 at 21:25











                  • Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.

                    – Stephen
                    Oct 18 '10 at 21:44











                  • it doesnt work for kshell

                    – g4ur4v
                    Jul 23 '14 at 10:28
















                  34














                  Yes, but don't do that. Use an array instead.



                  If you still insist on doing it that way...



                  $ foo1=123
                  $ bar=foo1
                  $ echo "${!bar}"
                  123





                  share|improve this answer



















                  • 5





                    +1 for but don't do that. Use an array instead.

                    – Nifle
                    Oct 18 '10 at 21:25











                  • Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.

                    – Stephen
                    Oct 18 '10 at 21:44











                  • it doesnt work for kshell

                    – g4ur4v
                    Jul 23 '14 at 10:28














                  34












                  34








                  34







                  Yes, but don't do that. Use an array instead.



                  If you still insist on doing it that way...



                  $ foo1=123
                  $ bar=foo1
                  $ echo "${!bar}"
                  123





                  share|improve this answer













                  Yes, but don't do that. Use an array instead.



                  If you still insist on doing it that way...



                  $ foo1=123
                  $ bar=foo1
                  $ echo "${!bar}"
                  123






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 18 '10 at 21:22









                  Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams

                  591k10710791183




                  591k10710791183








                  • 5





                    +1 for but don't do that. Use an array instead.

                    – Nifle
                    Oct 18 '10 at 21:25











                  • Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.

                    – Stephen
                    Oct 18 '10 at 21:44











                  • it doesnt work for kshell

                    – g4ur4v
                    Jul 23 '14 at 10:28














                  • 5





                    +1 for but don't do that. Use an array instead.

                    – Nifle
                    Oct 18 '10 at 21:25











                  • Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.

                    – Stephen
                    Oct 18 '10 at 21:44











                  • it doesnt work for kshell

                    – g4ur4v
                    Jul 23 '14 at 10:28








                  5




                  5





                  +1 for but don't do that. Use an array instead.

                  – Nifle
                  Oct 18 '10 at 21:25





                  +1 for but don't do that. Use an array instead.

                  – Nifle
                  Oct 18 '10 at 21:25













                  Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.

                  – Stephen
                  Oct 18 '10 at 21:44





                  Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.

                  – Stephen
                  Oct 18 '10 at 21:44













                  it doesnt work for kshell

                  – g4ur4v
                  Jul 23 '14 at 10:28





                  it doesnt work for kshell

                  – g4ur4v
                  Jul 23 '14 at 10:28













                  10














                  for I in 1 2 3 4 5; do
                  TMP="VAR$I"
                  echo ${!TMP}
                  done


                  I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.






                  share|improve this answer






























                    10














                    for I in 1 2 3 4 5; do
                    TMP="VAR$I"
                    echo ${!TMP}
                    done


                    I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.






                    share|improve this answer




























                      10












                      10








                      10







                      for I in 1 2 3 4 5; do
                      TMP="VAR$I"
                      echo ${!TMP}
                      done


                      I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.






                      share|improve this answer















                      for I in 1 2 3 4 5; do
                      TMP="VAR$I"
                      echo ${!TMP}
                      done


                      I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Oct 18 '10 at 21:37

























                      answered Oct 18 '10 at 21:27









                      Dummy00001Dummy00001

                      12.5k22454




                      12.5k22454























                          4














                          You should think about using bash arrays for this sort of work:



                          pax> set arr=(9 8 7 6)
                          pax> set idx=2
                          pax> echo ${arr[!idx]}
                          7





                          share|improve this answer




























                            4














                            You should think about using bash arrays for this sort of work:



                            pax> set arr=(9 8 7 6)
                            pax> set idx=2
                            pax> echo ${arr[!idx]}
                            7





                            share|improve this answer


























                              4












                              4








                              4







                              You should think about using bash arrays for this sort of work:



                              pax> set arr=(9 8 7 6)
                              pax> set idx=2
                              pax> echo ${arr[!idx]}
                              7





                              share|improve this answer













                              You should think about using bash arrays for this sort of work:



                              pax> set arr=(9 8 7 6)
                              pax> set idx=2
                              pax> echo ${arr[!idx]}
                              7






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Oct 18 '10 at 21:25









                              paxdiablopaxdiablo

                              643k17512671688




                              643k17512671688























                                  4














                                  For the case where you don't want to refactor your variables into arrays...



                                  One way...



                                  $ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done


                                  Another way...



                                  $ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done


                                  I haven't found a simpler way that works. For example, this does not work...



                                  $ for I in 1 2 3 4; do echo ${!VAR$I} ; done
                                  bash: ${!VAR$I}: bad substitution





                                  share|improve this answer



















                                  • 1





                                    Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.

                                    – nobar
                                    Apr 2 '14 at 16:16
















                                  4














                                  For the case where you don't want to refactor your variables into arrays...



                                  One way...



                                  $ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done


                                  Another way...



                                  $ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done


                                  I haven't found a simpler way that works. For example, this does not work...



                                  $ for I in 1 2 3 4; do echo ${!VAR$I} ; done
                                  bash: ${!VAR$I}: bad substitution





                                  share|improve this answer



















                                  • 1





                                    Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.

                                    – nobar
                                    Apr 2 '14 at 16:16














                                  4












                                  4








                                  4







                                  For the case where you don't want to refactor your variables into arrays...



                                  One way...



                                  $ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done


                                  Another way...



                                  $ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done


                                  I haven't found a simpler way that works. For example, this does not work...



                                  $ for I in 1 2 3 4; do echo ${!VAR$I} ; done
                                  bash: ${!VAR$I}: bad substitution





                                  share|improve this answer













                                  For the case where you don't want to refactor your variables into arrays...



                                  One way...



                                  $ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done


                                  Another way...



                                  $ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done


                                  I haven't found a simpler way that works. For example, this does not work...



                                  $ for I in 1 2 3 4; do echo ${!VAR$I} ; done
                                  bash: ${!VAR$I}: bad substitution






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Apr 2 '14 at 16:14









                                  nobarnobar

                                  27.1k1087100




                                  27.1k1087100








                                  • 1





                                    Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.

                                    – nobar
                                    Apr 2 '14 at 16:16














                                  • 1





                                    Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.

                                    – nobar
                                    Apr 2 '14 at 16:16








                                  1




                                  1





                                  Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.

                                  – nobar
                                  Apr 2 '14 at 16:16





                                  Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.

                                  – nobar
                                  Apr 2 '14 at 16:16











                                  3














                                  for I in {1..5}; do
                                  echo $((VAR$I))
                                  done





                                  share|improve this answer




























                                    3














                                    for I in {1..5}; do
                                    echo $((VAR$I))
                                    done





                                    share|improve this answer


























                                      3












                                      3








                                      3







                                      for I in {1..5}; do
                                      echo $((VAR$I))
                                      done





                                      share|improve this answer













                                      for I in {1..5}; do
                                      echo $((VAR$I))
                                      done






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 20 '16 at 20:42









                                      Ahmed MokhtarAhmed Mokhtar

                                      496




                                      496























                                          1














                                          Yes. See Advanced Bash-Scripting Guide






                                          share|improve this answer
























                                          • It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!

                                            – Charles Roberto Canato
                                            Dec 6 '17 at 19:16
















                                          1














                                          Yes. See Advanced Bash-Scripting Guide






                                          share|improve this answer
























                                          • It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!

                                            – Charles Roberto Canato
                                            Dec 6 '17 at 19:16














                                          1












                                          1








                                          1







                                          Yes. See Advanced Bash-Scripting Guide






                                          share|improve this answer













                                          Yes. See Advanced Bash-Scripting Guide







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Aug 17 '15 at 13:34









                                          gerardwgerardw

                                          3,1202228




                                          3,1202228













                                          • It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!

                                            – Charles Roberto Canato
                                            Dec 6 '17 at 19:16



















                                          • It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!

                                            – Charles Roberto Canato
                                            Dec 6 '17 at 19:16

















                                          It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!

                                          – Charles Roberto Canato
                                          Dec 6 '17 at 19:16





                                          It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!

                                          – Charles Roberto Canato
                                          Dec 6 '17 at 19:16











                                          0














                                          This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays






                                          share|improve this answer




























                                            0














                                            This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays






                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays






                                              share|improve this answer













                                              This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Oct 18 '10 at 21:26









                                              AnthonyAnthony

                                              5,52573568




                                              5,52573568















                                                  Popular posts from this blog

                                                  Wiesbaden

                                                  Marschland

                                                  Dieringhausen