Dynamic substitution of variables in bash The 2019 Stack Overflow Developer Survey Results Are InHow to insert bash variables in awk?Joining bash arguments into single string with spacesDouble variable substitution in bashHow to generate dynamic menu and make it usable?Can't pipe from echo to bash built-in read?How can I evaluate bash arguments in a string once variables have changedHow can I change a Bash variable name in a loop and then expand the changed name?Creating n variables in Bash without assigning them one by one?bash share array in “for do () & wait” loopHow can I export a variable in bash where the variable name is comprised of two variables?

Is it a good practice to use a static variable in a Test Class and use that in the actual class instead of Test.isRunningTest()?

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

What is the most efficient way to store a numeric range?

Ubuntu Server install with full GUI

Why can't devices on different VLANs, but on the same subnet, communicate?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Dynamic substitution of variables in bash

How to obtain a position of last non-zero element

Button changing its text & action. Good or terrible?

How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?

Relationship between Gromov-Witten and Taubes' Gromov invariant

If a sorcerer casts the Banishment spell on a PC while in Avernus, does the PC return to their home plane?

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

"as much details as you can remember"

What is this sharp, curved notch on my knife for?

Dropping list elements from nested list after evaluation

Does HR tell a hiring manager about salary negotiations?

Star Trek - X-shaped Item on Regula/Orbital Office Starbases

How do I free up internal storage if I don't have any apps downloaded?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Is an up-to-date browser secure on an out-of-date OS?

What is this business jet?

Why doesn't shell automatically fix "useless use of cat"?

Straighten subgroup lattice



Dynamic substitution of variables in bash



The 2019 Stack Overflow Developer Survey Results Are InHow to insert bash variables in awk?Joining bash arguments into single string with spacesDouble variable substitution in bashHow to generate dynamic menu and make it usable?Can't pipe from echo to bash built-in read?How can I evaluate bash arguments in a string once variables have changedHow can I change a Bash variable name in a loop and then expand the changed name?Creating n variables in Bash without assigning them one by one?bash share array in “for do () & wait” loopHow can I export a variable in bash where the variable name is comprised of two variables?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Honestly I don't even know how to ask this, or what are the proper terms lol



So what I'm trying to do, is to work on variables with variable names. I think I'd best describe it by actually showing it:
So what I had before was this



if [ -z $file_var ]; then
echo "empty argument"
fi
if [ -z $pw_var ]; then
echo "empty argument"
fi


So what I was trying to do was not have two if statements, but have just one and a for loop, something like this:



for i in file_var, pw_var;do
if [ -z $$i ]; then
echo "empty argument"
fi
done


Now this obviously doesn't work, so I'm wondering how is it properly done.










share|improve this question




























    0















    Honestly I don't even know how to ask this, or what are the proper terms lol



    So what I'm trying to do, is to work on variables with variable names. I think I'd best describe it by actually showing it:
    So what I had before was this



    if [ -z $file_var ]; then
    echo "empty argument"
    fi
    if [ -z $pw_var ]; then
    echo "empty argument"
    fi


    So what I was trying to do was not have two if statements, but have just one and a for loop, something like this:



    for i in file_var, pw_var;do
    if [ -z $$i ]; then
    echo "empty argument"
    fi
    done


    Now this obviously doesn't work, so I'm wondering how is it properly done.










    share|improve this question
























      0












      0








      0








      Honestly I don't even know how to ask this, or what are the proper terms lol



      So what I'm trying to do, is to work on variables with variable names. I think I'd best describe it by actually showing it:
      So what I had before was this



      if [ -z $file_var ]; then
      echo "empty argument"
      fi
      if [ -z $pw_var ]; then
      echo "empty argument"
      fi


      So what I was trying to do was not have two if statements, but have just one and a for loop, something like this:



      for i in file_var, pw_var;do
      if [ -z $$i ]; then
      echo "empty argument"
      fi
      done


      Now this obviously doesn't work, so I'm wondering how is it properly done.










      share|improve this question














      Honestly I don't even know how to ask this, or what are the proper terms lol



      So what I'm trying to do, is to work on variables with variable names. I think I'd best describe it by actually showing it:
      So what I had before was this



      if [ -z $file_var ]; then
      echo "empty argument"
      fi
      if [ -z $pw_var ]; then
      echo "empty argument"
      fi


      So what I was trying to do was not have two if statements, but have just one and a for loop, something like this:



      for i in file_var, pw_var;do
      if [ -z $$i ]; then
      echo "empty argument"
      fi
      done


      Now this obviously doesn't work, so I'm wondering how is it properly done.







      bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      user323587user323587

      542




      542




















          2 Answers
          2






          active

          oldest

          votes


















          3














          What you're trying to do is indirect expansion, which Bash supports using !:



          if [ -z "$!i" ] ; then



          If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




          In this case, "$!i" expands to whatever the value of the variable whose name is stored in i, and -z will test that indirectly-accessed value.




          Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name, and thereafter




          All references, assignments, and attribute modifications to name, except for those using or changing the -n attribute itself, are performed on the variable referenced by name’s value.




          So if you add



          declare -n i


          before your loop, it will mean that just



          for i in file_var pw_var
          do
          if [ -z "$i" ]
          then
          ...
          fi
          done


          will work as you wanted. Do note though that assignments to i (as opposed to its being set in a loop like this) will modify the target variable.




          Note also that there is no comma between items you're looping over in a Bash for loop like how you've written it in the question.






          share|improve this answer

























          • Thank you! That's exactly what I needed! Also thanks for the for loop tip :D

            – user323587
            4 hours ago


















          0














          This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.



          file_var=""
          pw_var=""

          for i in 'file_var' 'pw_var';do
          if [ -z $!i ]; then
          echo "$i is empty"
          fi
          done





          share|improve this answer








          New contributor




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




















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



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512008%2fdynamic-substitution-of-variables-in-bash%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            What you're trying to do is indirect expansion, which Bash supports using !:



            if [ -z "$!i" ] ; then



            If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




            In this case, "$!i" expands to whatever the value of the variable whose name is stored in i, and -z will test that indirectly-accessed value.




            Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name, and thereafter




            All references, assignments, and attribute modifications to name, except for those using or changing the -n attribute itself, are performed on the variable referenced by name’s value.




            So if you add



            declare -n i


            before your loop, it will mean that just



            for i in file_var pw_var
            do
            if [ -z "$i" ]
            then
            ...
            fi
            done


            will work as you wanted. Do note though that assignments to i (as opposed to its being set in a loop like this) will modify the target variable.




            Note also that there is no comma between items you're looping over in a Bash for loop like how you've written it in the question.






            share|improve this answer

























            • Thank you! That's exactly what I needed! Also thanks for the for loop tip :D

              – user323587
              4 hours ago















            3














            What you're trying to do is indirect expansion, which Bash supports using !:



            if [ -z "$!i" ] ; then



            If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




            In this case, "$!i" expands to whatever the value of the variable whose name is stored in i, and -z will test that indirectly-accessed value.




            Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name, and thereafter




            All references, assignments, and attribute modifications to name, except for those using or changing the -n attribute itself, are performed on the variable referenced by name’s value.




            So if you add



            declare -n i


            before your loop, it will mean that just



            for i in file_var pw_var
            do
            if [ -z "$i" ]
            then
            ...
            fi
            done


            will work as you wanted. Do note though that assignments to i (as opposed to its being set in a loop like this) will modify the target variable.




            Note also that there is no comma between items you're looping over in a Bash for loop like how you've written it in the question.






            share|improve this answer

























            • Thank you! That's exactly what I needed! Also thanks for the for loop tip :D

              – user323587
              4 hours ago













            3












            3








            3







            What you're trying to do is indirect expansion, which Bash supports using !:



            if [ -z "$!i" ] ; then



            If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




            In this case, "$!i" expands to whatever the value of the variable whose name is stored in i, and -z will test that indirectly-accessed value.




            Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name, and thereafter




            All references, assignments, and attribute modifications to name, except for those using or changing the -n attribute itself, are performed on the variable referenced by name’s value.




            So if you add



            declare -n i


            before your loop, it will mean that just



            for i in file_var pw_var
            do
            if [ -z "$i" ]
            then
            ...
            fi
            done


            will work as you wanted. Do note though that assignments to i (as opposed to its being set in a loop like this) will modify the target variable.




            Note also that there is no comma between items you're looping over in a Bash for loop like how you've written it in the question.






            share|improve this answer















            What you're trying to do is indirect expansion, which Bash supports using !:



            if [ -z "$!i" ] ; then



            If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.




            In this case, "$!i" expands to whatever the value of the variable whose name is stored in i, and -z will test that indirectly-accessed value.




            Alternatively, a nameref is a special type of variable that has this behaviour automatically. You make a nameref variable using declare -n name, and thereafter




            All references, assignments, and attribute modifications to name, except for those using or changing the -n attribute itself, are performed on the variable referenced by name’s value.




            So if you add



            declare -n i


            before your loop, it will mean that just



            for i in file_var pw_var
            do
            if [ -z "$i" ]
            then
            ...
            fi
            done


            will work as you wanted. Do note though that assignments to i (as opposed to its being set in a loop like this) will modify the target variable.




            Note also that there is no comma between items you're looping over in a Bash for loop like how you've written it in the question.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 4 hours ago

























            answered 4 hours ago









            Michael HomerMichael Homer

            50.8k8141177




            50.8k8141177












            • Thank you! That's exactly what I needed! Also thanks for the for loop tip :D

              – user323587
              4 hours ago

















            • Thank you! That's exactly what I needed! Also thanks for the for loop tip :D

              – user323587
              4 hours ago
















            Thank you! That's exactly what I needed! Also thanks for the for loop tip :D

            – user323587
            4 hours ago





            Thank you! That's exactly what I needed! Also thanks for the for loop tip :D

            – user323587
            4 hours ago













            0














            This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.



            file_var=""
            pw_var=""

            for i in 'file_var' 'pw_var';do
            if [ -z $!i ]; then
            echo "$i is empty"
            fi
            done





            share|improve this answer








            New contributor




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
























              0














              This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.



              file_var=""
              pw_var=""

              for i in 'file_var' 'pw_var';do
              if [ -z $!i ]; then
              echo "$i is empty"
              fi
              done





              share|improve this answer








              New contributor




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






















                0












                0








                0







                This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.



                file_var=""
                pw_var=""

                for i in 'file_var' 'pw_var';do
                if [ -z $!i ]; then
                echo "$i is empty"
                fi
                done





                share|improve this answer








                New contributor




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










                This code works, if file_var or pw_var are empty it will print that the specific variable is empty. You can tweak it to fit your usage.



                file_var=""
                pw_var=""

                for i in 'file_var' 'pw_var';do
                if [ -z $!i ]; then
                echo "$i is empty"
                fi
                done






                share|improve this answer








                New contributor




                Bob Dole 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 answer



                share|improve this answer






                New contributor




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









                answered 4 hours ago









                Bob DoleBob Dole

                661




                661




                New contributor




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





                New contributor





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






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



























                    draft saved

                    draft discarded
















































                    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%2f512008%2fdynamic-substitution-of-variables-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

                    Oświęcim Innehåll Historia | Källor | Externa länkar | Navigeringsmeny50°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.2213950°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.221393089658Nordisk familjebok, AuschwitzInsidan tro och existensJewish Community i OświęcimAuschwitz Jewish Center: MuseumAuschwitz Jewish Center

                    Valle di Casies Indice Geografia fisica | Origini del nome | Storia | Società | Amministrazione | Sport | Note | Bibliografia | Voci correlate | Altri progetti | Collegamenti esterni | Menu di navigazione46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)Sito istituzionaleAstat Censimento della popolazione 2011 - Determinazione della consistenza dei tre gruppi linguistici della Provincia Autonoma di Bolzano-Alto Adige - giugno 2012Numeri e fattiValle di CasiesDato IstatTabella dei gradi/giorno dei Comuni italiani raggruppati per Regione e Provincia26 agosto 1993, n. 412Heraldry of the World: GsiesStatistiche I.StatValCasies.comWikimedia CommonsWikimedia CommonsValle di CasiesSito ufficialeValle di CasiesMM14870458910042978-6

                    Typsetting diagram chases (with TikZ?) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)How to define the default vertical distance between nodes?Draw edge on arcNumerical conditional within tikz keys?TikZ: Drawing an arc from an intersection to an intersectionDrawing rectilinear curves in Tikz, aka an Etch-a-Sketch drawingLine up nested tikz enviroments or how to get rid of themHow to place nodes in an absolute coordinate system in tikzCommutative diagram with curve connecting between nodesTikz with standalone: pinning tikz coordinates to page cmDrawing a Decision Diagram with Tikz and layout manager