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

Are there any modern advantages of a fire piston?

Explain the objections to these measures against human trafficking

What is the lore-based reason that the Spectator has the Create Food and Water trait, instead of simply not requiring food and water?

Roman Numerals equation 1

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

How to prevent cleaner from hanging my lock screen in Ubuntu 16.04

Why did other German political parties disband so fast when Hitler was appointed chancellor?

Porting Linux to another platform requirements

What is the purpose of easy combat scenarios that don't need resource expenditure?

Can an insurance company drop you after receiving a bill and refusing to pay?

Propeller Fan - Array Modifier

Why exactly do action photographers need high fps burst cameras?

Cookies - Should the toggles be on?

How much mayhem could I cause as a sentient fish?

Highly technological aliens land nuclear fusion powered ships in medieval city and slaughter everyone, using swords?

Would a National Army of mercenaries be a feasible idea?

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

Error in a formula field

Why did the villain in the first Men in Black movie care about Earth's Cockroaches?

Can making a creature unable to attack after it has been assigned as an attacker remove it from combat?

Why would space fleets be aligned?

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

Why Normality assumption in linear regression

Why publish a research paper when a blog post or a lecture slide can have more citation count than a journal paper?



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 31 mins 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 39 mins 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


















          4














          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
            25 mins ago





















          1














          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









              4














              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
                25 mins ago


















              4














              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
                25 mins ago
















              4












              4








              4







              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 24 mins ago

























              answered 35 mins ago









              KusalanandaKusalananda

              133k17253416




              133k17253416













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

                – PaulB
                25 mins ago





















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

                – PaulB
                25 mins ago



















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

              – PaulB
              25 mins ago







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

              – PaulB
              25 mins ago















              1














              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




























                1














                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


























                  1












                  1








                  1







                  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 16 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 15 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

                              Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

                              Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

                              Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV