How to obtain the content of a variable in bash












0















I have a config file with this content:



config_scrm.sh



CDT_TRIGGER='69'
SR_TRIGGER='165'


In the main script I need to insert the content of CDT_TRIGGER (which is 69) in a variable called TRIGGER. The only thing I have to know I should get that exact variable content is another (local) variable called ELM='CDT'



So, in the main script, I execute:



main.sh



#!/bin/bash

source config_scrm.sh

(some calculations to obtain the value of variable ELM)

ELM='CDT'


I need TRIGGER to be 69.



If ELM='SR', I would need TRIGGER to be 165. And so on, there are way more values than showed in this excerpt.





What is not working:



MYSTRING='_TRIGGER'
TRIGGER=${$ELM$MYSTRING}


or



TRIGGER=`echo "$ELM$MYSTRING"`


or



TRIGGER=`$(echo "$ELM$MYSTRING")`









share|improve this question



























    0















    I have a config file with this content:



    config_scrm.sh



    CDT_TRIGGER='69'
    SR_TRIGGER='165'


    In the main script I need to insert the content of CDT_TRIGGER (which is 69) in a variable called TRIGGER. The only thing I have to know I should get that exact variable content is another (local) variable called ELM='CDT'



    So, in the main script, I execute:



    main.sh



    #!/bin/bash

    source config_scrm.sh

    (some calculations to obtain the value of variable ELM)

    ELM='CDT'


    I need TRIGGER to be 69.



    If ELM='SR', I would need TRIGGER to be 165. And so on, there are way more values than showed in this excerpt.





    What is not working:



    MYSTRING='_TRIGGER'
    TRIGGER=${$ELM$MYSTRING}


    or



    TRIGGER=`echo "$ELM$MYSTRING"`


    or



    TRIGGER=`$(echo "$ELM$MYSTRING")`









    share|improve this question

























      0












      0








      0








      I have a config file with this content:



      config_scrm.sh



      CDT_TRIGGER='69'
      SR_TRIGGER='165'


      In the main script I need to insert the content of CDT_TRIGGER (which is 69) in a variable called TRIGGER. The only thing I have to know I should get that exact variable content is another (local) variable called ELM='CDT'



      So, in the main script, I execute:



      main.sh



      #!/bin/bash

      source config_scrm.sh

      (some calculations to obtain the value of variable ELM)

      ELM='CDT'


      I need TRIGGER to be 69.



      If ELM='SR', I would need TRIGGER to be 165. And so on, there are way more values than showed in this excerpt.





      What is not working:



      MYSTRING='_TRIGGER'
      TRIGGER=${$ELM$MYSTRING}


      or



      TRIGGER=`echo "$ELM$MYSTRING"`


      or



      TRIGGER=`$(echo "$ELM$MYSTRING")`









      share|improve this question














      I have a config file with this content:



      config_scrm.sh



      CDT_TRIGGER='69'
      SR_TRIGGER='165'


      In the main script I need to insert the content of CDT_TRIGGER (which is 69) in a variable called TRIGGER. The only thing I have to know I should get that exact variable content is another (local) variable called ELM='CDT'



      So, in the main script, I execute:



      main.sh



      #!/bin/bash

      source config_scrm.sh

      (some calculations to obtain the value of variable ELM)

      ELM='CDT'


      I need TRIGGER to be 69.



      If ELM='SR', I would need TRIGGER to be 165. And so on, there are way more values than showed in this excerpt.





      What is not working:



      MYSTRING='_TRIGGER'
      TRIGGER=${$ELM$MYSTRING}


      or



      TRIGGER=`echo "$ELM$MYSTRING"`


      or



      TRIGGER=`$(echo "$ELM$MYSTRING")`






      linux bash environment-variables






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 14 at 9:06









      UxioUxio

      153




      153






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Although $ELM$MYSTRING expands to the name you want, TRIGGER=${$ELM$MYSTRING} doesn't work because here you need parameter expansion to occur twice: "inner" variables first, then the "outer" one.



          Your tries with command substitution would make little more sense if you actually used the "outer" $ to try to trigger additional parameter expansion; but even then they would fail for similar reason as the above.



          Well, you can have parameter expansion in two separate steps. You need eval for this:



          eval 'TRIGGER=$'"$ELM$MYSTRING"


          At first the variables within "" are expanded normally, everything within '' is left intact. Then eval performs additional parsing of the resulting string which is now exactly TRIGGER=$CDT_TRIGGER. The solution is portable, although you should be careful with eval. Please see Why should eval be avoided in Bash, and what should I use instead?



          In Bash there's a safer (yet not portable) way to do what you want:



          wholename=$ELM$MYSTRING    # we need this in a single variable
          TRIGGER=${!wholename}


          Side note: consider lowercase names for your variables.






          share|improve this answer
























          • Since the ${!wholename} version is working perfectly, I'll stick to it. Regards.

            – Uxio
            Jan 14 at 12:57











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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%2fsuperuser.com%2fquestions%2f1394028%2fhow-to-obtain-the-content-of-a-variable-in-bash%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









          0














          Although $ELM$MYSTRING expands to the name you want, TRIGGER=${$ELM$MYSTRING} doesn't work because here you need parameter expansion to occur twice: "inner" variables first, then the "outer" one.



          Your tries with command substitution would make little more sense if you actually used the "outer" $ to try to trigger additional parameter expansion; but even then they would fail for similar reason as the above.



          Well, you can have parameter expansion in two separate steps. You need eval for this:



          eval 'TRIGGER=$'"$ELM$MYSTRING"


          At first the variables within "" are expanded normally, everything within '' is left intact. Then eval performs additional parsing of the resulting string which is now exactly TRIGGER=$CDT_TRIGGER. The solution is portable, although you should be careful with eval. Please see Why should eval be avoided in Bash, and what should I use instead?



          In Bash there's a safer (yet not portable) way to do what you want:



          wholename=$ELM$MYSTRING    # we need this in a single variable
          TRIGGER=${!wholename}


          Side note: consider lowercase names for your variables.






          share|improve this answer
























          • Since the ${!wholename} version is working perfectly, I'll stick to it. Regards.

            – Uxio
            Jan 14 at 12:57
















          0














          Although $ELM$MYSTRING expands to the name you want, TRIGGER=${$ELM$MYSTRING} doesn't work because here you need parameter expansion to occur twice: "inner" variables first, then the "outer" one.



          Your tries with command substitution would make little more sense if you actually used the "outer" $ to try to trigger additional parameter expansion; but even then they would fail for similar reason as the above.



          Well, you can have parameter expansion in two separate steps. You need eval for this:



          eval 'TRIGGER=$'"$ELM$MYSTRING"


          At first the variables within "" are expanded normally, everything within '' is left intact. Then eval performs additional parsing of the resulting string which is now exactly TRIGGER=$CDT_TRIGGER. The solution is portable, although you should be careful with eval. Please see Why should eval be avoided in Bash, and what should I use instead?



          In Bash there's a safer (yet not portable) way to do what you want:



          wholename=$ELM$MYSTRING    # we need this in a single variable
          TRIGGER=${!wholename}


          Side note: consider lowercase names for your variables.






          share|improve this answer
























          • Since the ${!wholename} version is working perfectly, I'll stick to it. Regards.

            – Uxio
            Jan 14 at 12:57














          0












          0








          0







          Although $ELM$MYSTRING expands to the name you want, TRIGGER=${$ELM$MYSTRING} doesn't work because here you need parameter expansion to occur twice: "inner" variables first, then the "outer" one.



          Your tries with command substitution would make little more sense if you actually used the "outer" $ to try to trigger additional parameter expansion; but even then they would fail for similar reason as the above.



          Well, you can have parameter expansion in two separate steps. You need eval for this:



          eval 'TRIGGER=$'"$ELM$MYSTRING"


          At first the variables within "" are expanded normally, everything within '' is left intact. Then eval performs additional parsing of the resulting string which is now exactly TRIGGER=$CDT_TRIGGER. The solution is portable, although you should be careful with eval. Please see Why should eval be avoided in Bash, and what should I use instead?



          In Bash there's a safer (yet not portable) way to do what you want:



          wholename=$ELM$MYSTRING    # we need this in a single variable
          TRIGGER=${!wholename}


          Side note: consider lowercase names for your variables.






          share|improve this answer













          Although $ELM$MYSTRING expands to the name you want, TRIGGER=${$ELM$MYSTRING} doesn't work because here you need parameter expansion to occur twice: "inner" variables first, then the "outer" one.



          Your tries with command substitution would make little more sense if you actually used the "outer" $ to try to trigger additional parameter expansion; but even then they would fail for similar reason as the above.



          Well, you can have parameter expansion in two separate steps. You need eval for this:



          eval 'TRIGGER=$'"$ELM$MYSTRING"


          At first the variables within "" are expanded normally, everything within '' is left intact. Then eval performs additional parsing of the resulting string which is now exactly TRIGGER=$CDT_TRIGGER. The solution is portable, although you should be careful with eval. Please see Why should eval be avoided in Bash, and what should I use instead?



          In Bash there's a safer (yet not portable) way to do what you want:



          wholename=$ELM$MYSTRING    # we need this in a single variable
          TRIGGER=${!wholename}


          Side note: consider lowercase names for your variables.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 14 at 10:09









          Kamil MaciorowskiKamil Maciorowski

          26.3k155680




          26.3k155680













          • Since the ${!wholename} version is working perfectly, I'll stick to it. Regards.

            – Uxio
            Jan 14 at 12:57



















          • Since the ${!wholename} version is working perfectly, I'll stick to it. Regards.

            – Uxio
            Jan 14 at 12:57

















          Since the ${!wholename} version is working perfectly, I'll stick to it. Regards.

          – Uxio
          Jan 14 at 12:57





          Since the ${!wholename} version is working perfectly, I'll stick to it. Regards.

          – Uxio
          Jan 14 at 12:57


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • 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%2fsuperuser.com%2fquestions%2f1394028%2fhow-to-obtain-the-content-of-a-variable-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

          flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

          Mangá

          Eduardo VII do Reino Unido