Indirectly access environment variable [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












4
















This question already has an answer here:



  • Variable substitution with an exclamation mark in bash

    1 answer



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.










share|improve this question















marked as duplicate by terdon 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();

);
);
);
Mar 1 at 18:22


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.






















    4
















    This question already has an answer here:



    • Variable substitution with an exclamation mark in bash

      1 answer



    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.










    share|improve this question















    marked as duplicate by terdon 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();

    );
    );
    );
    Mar 1 at 18:22


    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.




















      4












      4








      4









      This question already has an answer here:



      • Variable substitution with an exclamation mark in bash

        1 answer



      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.










      share|improve this question

















      This question already has an answer here:



      • Variable substitution with an exclamation mark in bash

        1 answer



      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.





      This question already has an answer here:



      • Variable substitution with an exclamation mark in bash

        1 answer







      bash variable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 1 at 17:28









      Rui F Ribeiro

      41.8k1483142




      41.8k1483142










      asked Mar 1 at 10:21









      PaulBPaulB

      1234




      1234




      marked as duplicate by terdon 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();

      );
      );
      );
      Mar 1 at 18:22


      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 terdon 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();

      );
      );
      );
      Mar 1 at 18:22


      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


















          10














          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
            Mar 1 at 10:36



















          4














          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














            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





























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              10














              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
                Mar 1 at 10:36
















              10














              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
                Mar 1 at 10:36














              10












              10








              10







              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 Mar 1 at 10:37

























              answered Mar 1 at 10:26









              KusalanandaKusalananda

              138k17258426




              138k17258426












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

                – PaulB
                Mar 1 at 10:36


















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

                – PaulB
                Mar 1 at 10:36

















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

              – PaulB
              Mar 1 at 10:36






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

              – PaulB
              Mar 1 at 10:36














              4














              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



























                4














                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

























                  4












                  4








                  4







                  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 Mar 1 at 10:45









                  Stéphane ChazelasStéphane Chazelas

                  312k57589946




                  312k57589946





















                      1














                      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



























                        1














                        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

























                          1












                          1








                          1







                          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 Mar 1 at 10:46









                          katoshkatosh

                          1619




                          1619












                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)