Indirectly access environment variableNeed to set a variable with “[]”Serialize shell variable in bash or...

How would an AI self awareness kill switch work?

What is the most triangles you can make from a capital "H" and 3 straight lines?

How do I say "Brexit" in Latin?

Who is this Ant Woman character in this image alongside the Wasp?

Why do stocks necessarily drop during a recession?

How to avoid being sexist when trying to employ someone to function in a very sexist environment?

Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?

Pronunciation of umlaut vowels in the history of German

Can we use the stored gravitational potential energy of a building to produce power?

How to deal with an incendiary email that was recalled

Writing a character who is going through a civilizing process without overdoing it?

Why Normality assumption in linear regression

Differentiate between Local and Global Unitaries

Indirectly access environment variable

How much mayhem could I cause as a sentient fish?

Lick explanation

Traveling through the asteriod belt?

Can a person refuse a presidential pardon?

What kind of hardware implements Fourier transform?

How can animals be objects of ethics without being subjects as well?

How to remove lines through the legend markers in ListPlot?

Why are the books in the Game of Thrones citadel library shelved spine inwards?

Strange Sign on Lab Door

How can my powered armor quickly replace its ceramic plates?



Indirectly access environment variable


Need to set a variable with “[]”Serialize shell variable in bash or zshshell script unable to set environment variable with the grepped valueGet the latest value of an environment variable is a bash shell scriptcopy array with array name inside string in bashEvaluate command stored in Environment variableBash - assign array into variable as stringVariable in while loop only check on initial execution?Environment variable not visible after `sudo su`Process substitution inside a subshell to set a variable













2















Given I have in a bash script



ev=USER


How can I get the environment variable value for $USER using ev?



Tried naively doing:



echo ${"$"$ev}


which results in bad substitution.



I'd expect to get back whatever the value of $USER is.



thanks










share|improve this question









New contributor




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

























    2















    Given I have in a bash script



    ev=USER


    How can I get the environment variable value for $USER using ev?



    Tried naively doing:



    echo ${"$"$ev}


    which results in bad substitution.



    I'd expect to get back whatever the value of $USER is.



    thanks










    share|improve this question









    New contributor




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























      2












      2








      2








      Given I have in a bash script



      ev=USER


      How can I get the environment variable value for $USER using ev?



      Tried naively doing:



      echo ${"$"$ev}


      which results in bad substitution.



      I'd expect to get back whatever the value of $USER is.



      thanks










      share|improve this question









      New contributor




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












      Given I have in a bash script



      ev=USER


      How can I get the environment variable value for $USER using ev?



      Tried naively doing:



      echo ${"$"$ev}


      which results in bad substitution.



      I'd expect to get back whatever the value of $USER is.



      thanks







      bash variable






      share|improve this question









      New contributor




      PaulB 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




      PaulB 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








      edited 1 hour ago









      Kusalananda

      133k17253416




      133k17253416






      New contributor




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









      asked 1 hour ago









      PaulBPaulB

      1134




      1134




      New contributor




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





      New contributor





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






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






















          3 Answers
          3






          active

          oldest

          votes


















          5














          By using an indirect expansion (also sometimes called "variable indirection"),



          ev=USER
          printf '%sn' "${!ev}"


          This is described in the bash (5.0) manual, in the section titled "Parameter Expansion".



          Or, by making ev a name reference (requires bash 4.3+),



          declare -n ev=USER
          printf '%sn' "$ev"


          This is described in the bash (5.0) manual, just before the section called "Positional Parameters".






          share|improve this answer


























          • Perfect thanks ... env_val="${!ev}"

            – PaulB
            1 hour ago





















          2














          If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:



          printenv -- "$ev"


          For shell variables, with any Bourne-like shell, you can do:



          eval 'printf "%sn" "${'"$ev"}'}"'


          Or with zsh:



          printf '%sn' "${(P)ev}"


          Or with bash:



          printf '%sn' "${!ev}"


          All 3 are arbitrary command injection vulnerabilities if the content of $ev is not under your control.






          share|improve this answer































            0














            You can also evaluate the command after the vale for $ev has been substituted:



            eval echo "$"$ev


            The part "$"$ev resolves to $USER so eval executes echo $USER.






            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',
              autoActivateHeartbeat: false,
              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
              });


              }
              });






              PaulB 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%2f503732%2findirectly-access-environment-variable%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              By using an indirect expansion (also sometimes called "variable indirection"),



              ev=USER
              printf '%sn' "${!ev}"


              This is described in the bash (5.0) manual, in the section titled "Parameter Expansion".



              Or, by making ev a name reference (requires bash 4.3+),



              declare -n ev=USER
              printf '%sn' "$ev"


              This is described in the bash (5.0) manual, just before the section called "Positional Parameters".






              share|improve this answer


























              • Perfect thanks ... env_val="${!ev}"

                – PaulB
                1 hour ago


















              5














              By using an indirect expansion (also sometimes called "variable indirection"),



              ev=USER
              printf '%sn' "${!ev}"


              This is described in the bash (5.0) manual, in the section titled "Parameter Expansion".



              Or, by making ev a name reference (requires bash 4.3+),



              declare -n ev=USER
              printf '%sn' "$ev"


              This is described in the bash (5.0) manual, just before the section called "Positional Parameters".






              share|improve this answer


























              • Perfect thanks ... env_val="${!ev}"

                – PaulB
                1 hour ago
















              5












              5








              5







              By using an indirect expansion (also sometimes called "variable indirection"),



              ev=USER
              printf '%sn' "${!ev}"


              This is described in the bash (5.0) manual, in the section titled "Parameter Expansion".



              Or, by making ev a name reference (requires bash 4.3+),



              declare -n ev=USER
              printf '%sn' "$ev"


              This is described in the bash (5.0) manual, just before the section called "Positional Parameters".






              share|improve this answer















              By using an indirect expansion (also sometimes called "variable indirection"),



              ev=USER
              printf '%sn' "${!ev}"


              This is described in the bash (5.0) manual, in the section titled "Parameter Expansion".



              Or, by making ev a name reference (requires bash 4.3+),



              declare -n ev=USER
              printf '%sn' "$ev"


              This is described in the bash (5.0) manual, just before the section called "Positional Parameters".







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 hour ago

























              answered 1 hour ago









              KusalanandaKusalananda

              133k17253416




              133k17253416













              • Perfect thanks ... env_val="${!ev}"

                – PaulB
                1 hour ago





















              • Perfect thanks ... env_val="${!ev}"

                – PaulB
                1 hour ago



















              Perfect thanks ... env_val="${!ev}"

              – PaulB
              1 hour ago







              Perfect thanks ... env_val="${!ev}"

              – PaulB
              1 hour ago















              2














              If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:



              printenv -- "$ev"


              For shell variables, with any Bourne-like shell, you can do:



              eval 'printf "%sn" "${'"$ev"}'}"'


              Or with zsh:



              printf '%sn' "${(P)ev}"


              Or with bash:



              printf '%sn' "${!ev}"


              All 3 are arbitrary command injection vulnerabilities if the content of $ev is not under your control.






              share|improve this answer




























                2














                If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:



                printenv -- "$ev"


                For shell variables, with any Bourne-like shell, you can do:



                eval 'printf "%sn" "${'"$ev"}'}"'


                Or with zsh:



                printf '%sn' "${(P)ev}"


                Or with bash:



                printf '%sn' "${!ev}"


                All 3 are arbitrary command injection vulnerabilities if the content of $ev is not under your control.






                share|improve this answer


























                  2












                  2








                  2







                  If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:



                  printenv -- "$ev"


                  For shell variables, with any Bourne-like shell, you can do:



                  eval 'printf "%sn" "${'"$ev"}'}"'


                  Or with zsh:



                  printf '%sn' "${(P)ev}"


                  Or with bash:



                  printf '%sn' "${!ev}"


                  All 3 are arbitrary command injection vulnerabilities if the content of $ev is not under your control.






                  share|improve this answer













                  If it's only about environment variables, as opposed to shell variables, then on most systems, you can use:



                  printenv -- "$ev"


                  For shell variables, with any Bourne-like shell, you can do:



                  eval 'printf "%sn" "${'"$ev"}'}"'


                  Or with zsh:



                  printf '%sn' "${(P)ev}"


                  Or with bash:



                  printf '%sn' "${!ev}"


                  All 3 are arbitrary command injection vulnerabilities if the content of $ev is not under your control.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 57 mins ago









                  Stéphane ChazelasStéphane Chazelas

                  308k57581939




                  308k57581939























                      0














                      You can also evaluate the command after the vale for $ev has been substituted:



                      eval echo "$"$ev


                      The part "$"$ev resolves to $USER so eval executes echo $USER.






                      share|improve this answer




























                        0














                        You can also evaluate the command after the vale for $ev has been substituted:



                        eval echo "$"$ev


                        The part "$"$ev resolves to $USER so eval executes echo $USER.






                        share|improve this answer


























                          0












                          0








                          0







                          You can also evaluate the command after the vale for $ev has been substituted:



                          eval echo "$"$ev


                          The part "$"$ev resolves to $USER so eval executes echo $USER.






                          share|improve this answer













                          You can also evaluate the command after the vale for $ev has been substituted:



                          eval echo "$"$ev


                          The part "$"$ev resolves to $USER so eval executes echo $USER.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 56 mins ago









                          katoshkatosh

                          425




                          425






















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










                              draft saved

                              draft discarded


















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













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












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
















                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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%2funix.stackexchange.com%2fquestions%2f503732%2findirectly-access-environment-variable%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

                              “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                              How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                              夢乃愛華...