Reference bash variable from a variable [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



How can I reference a variable in bash based on another variable? Let me setup the example:



package="foobar"

# the variable I wish to reference is $foobar_darwin_amd64
# thus trying:
echo "$package_darwin_amd64"


But this does not work.







share|improve this question












marked as duplicate by Barmar, Jeff Schaller, G-Man, Kusalananda 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();

);
);
);
Feb 1 at 6:44


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



    How can I reference a variable in bash based on another variable? Let me setup the example:



    package="foobar"

    # the variable I wish to reference is $foobar_darwin_amd64
    # thus trying:
    echo "$package_darwin_amd64"


    But this does not work.







    share|improve this question












    marked as duplicate by Barmar, Jeff Schaller, G-Man, Kusalananda 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();

    );
    );
    );
    Feb 1 at 6:44


    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



      How can I reference a variable in bash based on another variable? Let me setup the example:



      package="foobar"

      # the variable I wish to reference is $foobar_darwin_amd64
      # thus trying:
      echo "$package_darwin_amd64"


      But this does not work.







      share|improve this question













      This question already has an answer here:



      • How to do indirect variable evaluation

        5 answers



      How can I reference a variable in bash based on another variable? Let me setup the example:



      package="foobar"

      # the variable I wish to reference is $foobar_darwin_amd64
      # thus trying:
      echo "$package_darwin_amd64"


      But this does not work.





      This question already has an answer here:



      • How to do indirect variable evaluation

        5 answers









      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 31 at 23:27









      Justin

      1112




      1112




      marked as duplicate by Barmar, Jeff Schaller, G-Man, Kusalananda 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();

      );
      );
      );
      Feb 1 at 6:44


      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 Barmar, Jeff Schaller, G-Man, Kusalananda 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();

      );
      );
      );
      Feb 1 at 6:44


      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.






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote













          If you shell supports the $!varname form of indirect references, you can do (as suggested by @Barmar):



          $ foobar_darwin_amd64=pinto
          $ package=foobar
          $ varname="$package_darwin_amd64"
          $ echo $!varname
          pinto


          Otherwise, you can use eval:



          $ foobar_darwin_amd64=pinto
          $ package=foobar
          $ eval echo $$package_darwin_amd64
          pinto


          That said, using eval has some risks associated with it, see this link for more discussion.






          share|improve this answer






















          • Why use eval when you can use an indirect reference?
            – Barmar
            Jan 31 at 23:33

















          up vote
          1
          down vote













          Here is the solution that worked for me:



          VARNAME="$package_darwin_amd64"
          echo "$!VARNAME"





          share|improve this answer



























            up vote
            1
            down vote













            In bash v4 you can use a "nameref"



            $ foobar_darwin_amd64=pinto
            $ package=foobar
            $ declare -n var=$package_darwin_amd64
            $ echo $var
            pinto





            share|improve this answer



























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              If you shell supports the $!varname form of indirect references, you can do (as suggested by @Barmar):



              $ foobar_darwin_amd64=pinto
              $ package=foobar
              $ varname="$package_darwin_amd64"
              $ echo $!varname
              pinto


              Otherwise, you can use eval:



              $ foobar_darwin_amd64=pinto
              $ package=foobar
              $ eval echo $$package_darwin_amd64
              pinto


              That said, using eval has some risks associated with it, see this link for more discussion.






              share|improve this answer






















              • Why use eval when you can use an indirect reference?
                – Barmar
                Jan 31 at 23:33














              up vote
              2
              down vote













              If you shell supports the $!varname form of indirect references, you can do (as suggested by @Barmar):



              $ foobar_darwin_amd64=pinto
              $ package=foobar
              $ varname="$package_darwin_amd64"
              $ echo $!varname
              pinto


              Otherwise, you can use eval:



              $ foobar_darwin_amd64=pinto
              $ package=foobar
              $ eval echo $$package_darwin_amd64
              pinto


              That said, using eval has some risks associated with it, see this link for more discussion.






              share|improve this answer






















              • Why use eval when you can use an indirect reference?
                – Barmar
                Jan 31 at 23:33












              up vote
              2
              down vote










              up vote
              2
              down vote









              If you shell supports the $!varname form of indirect references, you can do (as suggested by @Barmar):



              $ foobar_darwin_amd64=pinto
              $ package=foobar
              $ varname="$package_darwin_amd64"
              $ echo $!varname
              pinto


              Otherwise, you can use eval:



              $ foobar_darwin_amd64=pinto
              $ package=foobar
              $ eval echo $$package_darwin_amd64
              pinto


              That said, using eval has some risks associated with it, see this link for more discussion.






              share|improve this answer














              If you shell supports the $!varname form of indirect references, you can do (as suggested by @Barmar):



              $ foobar_darwin_amd64=pinto
              $ package=foobar
              $ varname="$package_darwin_amd64"
              $ echo $!varname
              pinto


              Otherwise, you can use eval:



              $ foobar_darwin_amd64=pinto
              $ package=foobar
              $ eval echo $$package_darwin_amd64
              pinto


              That said, using eval has some risks associated with it, see this link for more discussion.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 31 at 23:46

























              answered Jan 31 at 23:31









              Andy Dalton

              4,7561520




              4,7561520











              • Why use eval when you can use an indirect reference?
                – Barmar
                Jan 31 at 23:33
















              • Why use eval when you can use an indirect reference?
                – Barmar
                Jan 31 at 23:33















              Why use eval when you can use an indirect reference?
              – Barmar
              Jan 31 at 23:33




              Why use eval when you can use an indirect reference?
              – Barmar
              Jan 31 at 23:33












              up vote
              1
              down vote













              Here is the solution that worked for me:



              VARNAME="$package_darwin_amd64"
              echo "$!VARNAME"





              share|improve this answer
























                up vote
                1
                down vote













                Here is the solution that worked for me:



                VARNAME="$package_darwin_amd64"
                echo "$!VARNAME"





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Here is the solution that worked for me:



                  VARNAME="$package_darwin_amd64"
                  echo "$!VARNAME"





                  share|improve this answer












                  Here is the solution that worked for me:



                  VARNAME="$package_darwin_amd64"
                  echo "$!VARNAME"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 31 at 23:49









                  Justin

                  1112




                  1112




















                      up vote
                      1
                      down vote













                      In bash v4 you can use a "nameref"



                      $ foobar_darwin_amd64=pinto
                      $ package=foobar
                      $ declare -n var=$package_darwin_amd64
                      $ echo $var
                      pinto





                      share|improve this answer
























                        up vote
                        1
                        down vote













                        In bash v4 you can use a "nameref"



                        $ foobar_darwin_amd64=pinto
                        $ package=foobar
                        $ declare -n var=$package_darwin_amd64
                        $ echo $var
                        pinto





                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          In bash v4 you can use a "nameref"



                          $ foobar_darwin_amd64=pinto
                          $ package=foobar
                          $ declare -n var=$package_darwin_amd64
                          $ echo $var
                          pinto





                          share|improve this answer












                          In bash v4 you can use a "nameref"



                          $ foobar_darwin_amd64=pinto
                          $ package=foobar
                          $ declare -n var=$package_darwin_amd64
                          $ echo $var
                          pinto






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 1 at 0:32









                          glenn jackman

                          46.5k265103




                          46.5k265103












                              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?