import environment variables in a bash script












2















I set some environment variables in a terminal, and then run my script. How can I pull in the variables in the script? I need to know their values. Simply referring to them as $MY_VAR1 doesn't work; it is empty.










share|improve this question

























  • Possible duplicate of How can I pass an environment variable to a script?

    – Julien Lopez
    Jan 17 at 22:22
















2















I set some environment variables in a terminal, and then run my script. How can I pull in the variables in the script? I need to know their values. Simply referring to them as $MY_VAR1 doesn't work; it is empty.










share|improve this question

























  • Possible duplicate of How can I pass an environment variable to a script?

    – Julien Lopez
    Jan 17 at 22:22














2












2








2








I set some environment variables in a terminal, and then run my script. How can I pull in the variables in the script? I need to know their values. Simply referring to them as $MY_VAR1 doesn't work; it is empty.










share|improve this question
















I set some environment variables in a terminal, and then run my script. How can I pull in the variables in the script? I need to know their values. Simply referring to them as $MY_VAR1 doesn't work; it is empty.







bash shell-script scripting environment-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 17 at 23:56









Jeff Schaller

42.3k1157134




42.3k1157134










asked Jan 17 at 20:53









MarkMark

5052624




5052624













  • Possible duplicate of How can I pass an environment variable to a script?

    – Julien Lopez
    Jan 17 at 22:22



















  • Possible duplicate of How can I pass an environment variable to a script?

    – Julien Lopez
    Jan 17 at 22:22

















Possible duplicate of How can I pass an environment variable to a script?

– Julien Lopez
Jan 17 at 22:22





Possible duplicate of How can I pass an environment variable to a script?

– Julien Lopez
Jan 17 at 22:22










2 Answers
2






active

oldest

votes


















7














If the variables are truly environment variables (i.e., they've been exported with export) in the environment that invokes your script, then they would be available in your script. That they aren't suggests that you haven't exported them, or that you run the script from an environment where they simply don't exist even as shell variables.



Example:



$ cat script.sh
#!/bin/sh

echo "$hello"




$ sh script.sh


(one empty line of output since hello doesn't exist anywhere)





$ hello="hi there"
$ sh script.sh


(still only an empty line as output as hello is only a shell variable, not an environment variable)



$ export hello
$ sh script.sh
hi there


Alternatively, to set the environment variable just for this script and not in the calling environment:



$ hello="sorry, I'm busy" sh script.sh
sorry, I'm busy




$ env hello="this works too" sh script.sh
this works too





share|improve this answer

































    1














    You need to ensure you export the environment variables you want to have access to in your script before you invoke the script. IE:



    Unix> export MY_TEMP=/tmp
    Unix> some_script.sh


    Now some_script.sh would have access to $MY_TEMP -- when you invoke a shell script, you get a new environment, with only exported variables, unless you "source" it by preceeding the script command with a period (".") and a space, then your script name:



    Unix>  . some_script.sh  # runs in current environment


    Debugging tip: Include near the top of your script the set command to see what variables your script can see.






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


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495161%2fimport-environment-variables-in-a-bash-script%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









      7














      If the variables are truly environment variables (i.e., they've been exported with export) in the environment that invokes your script, then they would be available in your script. That they aren't suggests that you haven't exported them, or that you run the script from an environment where they simply don't exist even as shell variables.



      Example:



      $ cat script.sh
      #!/bin/sh

      echo "$hello"




      $ sh script.sh


      (one empty line of output since hello doesn't exist anywhere)





      $ hello="hi there"
      $ sh script.sh


      (still only an empty line as output as hello is only a shell variable, not an environment variable)



      $ export hello
      $ sh script.sh
      hi there


      Alternatively, to set the environment variable just for this script and not in the calling environment:



      $ hello="sorry, I'm busy" sh script.sh
      sorry, I'm busy




      $ env hello="this works too" sh script.sh
      this works too





      share|improve this answer






























        7














        If the variables are truly environment variables (i.e., they've been exported with export) in the environment that invokes your script, then they would be available in your script. That they aren't suggests that you haven't exported them, or that you run the script from an environment where they simply don't exist even as shell variables.



        Example:



        $ cat script.sh
        #!/bin/sh

        echo "$hello"




        $ sh script.sh


        (one empty line of output since hello doesn't exist anywhere)





        $ hello="hi there"
        $ sh script.sh


        (still only an empty line as output as hello is only a shell variable, not an environment variable)



        $ export hello
        $ sh script.sh
        hi there


        Alternatively, to set the environment variable just for this script and not in the calling environment:



        $ hello="sorry, I'm busy" sh script.sh
        sorry, I'm busy




        $ env hello="this works too" sh script.sh
        this works too





        share|improve this answer




























          7












          7








          7







          If the variables are truly environment variables (i.e., they've been exported with export) in the environment that invokes your script, then they would be available in your script. That they aren't suggests that you haven't exported them, or that you run the script from an environment where they simply don't exist even as shell variables.



          Example:



          $ cat script.sh
          #!/bin/sh

          echo "$hello"




          $ sh script.sh


          (one empty line of output since hello doesn't exist anywhere)





          $ hello="hi there"
          $ sh script.sh


          (still only an empty line as output as hello is only a shell variable, not an environment variable)



          $ export hello
          $ sh script.sh
          hi there


          Alternatively, to set the environment variable just for this script and not in the calling environment:



          $ hello="sorry, I'm busy" sh script.sh
          sorry, I'm busy




          $ env hello="this works too" sh script.sh
          this works too





          share|improve this answer















          If the variables are truly environment variables (i.e., they've been exported with export) in the environment that invokes your script, then they would be available in your script. That they aren't suggests that you haven't exported them, or that you run the script from an environment where they simply don't exist even as shell variables.



          Example:



          $ cat script.sh
          #!/bin/sh

          echo "$hello"




          $ sh script.sh


          (one empty line of output since hello doesn't exist anywhere)





          $ hello="hi there"
          $ sh script.sh


          (still only an empty line as output as hello is only a shell variable, not an environment variable)



          $ export hello
          $ sh script.sh
          hi there


          Alternatively, to set the environment variable just for this script and not in the calling environment:



          $ hello="sorry, I'm busy" sh script.sh
          sorry, I'm busy




          $ env hello="this works too" sh script.sh
          this works too






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 17 at 21:22

























          answered Jan 17 at 20:55









          KusalanandaKusalananda

          132k17252416




          132k17252416

























              1














              You need to ensure you export the environment variables you want to have access to in your script before you invoke the script. IE:



              Unix> export MY_TEMP=/tmp
              Unix> some_script.sh


              Now some_script.sh would have access to $MY_TEMP -- when you invoke a shell script, you get a new environment, with only exported variables, unless you "source" it by preceeding the script command with a period (".") and a space, then your script name:



              Unix>  . some_script.sh  # runs in current environment


              Debugging tip: Include near the top of your script the set command to see what variables your script can see.






              share|improve this answer




























                1














                You need to ensure you export the environment variables you want to have access to in your script before you invoke the script. IE:



                Unix> export MY_TEMP=/tmp
                Unix> some_script.sh


                Now some_script.sh would have access to $MY_TEMP -- when you invoke a shell script, you get a new environment, with only exported variables, unless you "source" it by preceeding the script command with a period (".") and a space, then your script name:



                Unix>  . some_script.sh  # runs in current environment


                Debugging tip: Include near the top of your script the set command to see what variables your script can see.






                share|improve this answer


























                  1












                  1








                  1







                  You need to ensure you export the environment variables you want to have access to in your script before you invoke the script. IE:



                  Unix> export MY_TEMP=/tmp
                  Unix> some_script.sh


                  Now some_script.sh would have access to $MY_TEMP -- when you invoke a shell script, you get a new environment, with only exported variables, unless you "source" it by preceeding the script command with a period (".") and a space, then your script name:



                  Unix>  . some_script.sh  # runs in current environment


                  Debugging tip: Include near the top of your script the set command to see what variables your script can see.






                  share|improve this answer













                  You need to ensure you export the environment variables you want to have access to in your script before you invoke the script. IE:



                  Unix> export MY_TEMP=/tmp
                  Unix> some_script.sh


                  Now some_script.sh would have access to $MY_TEMP -- when you invoke a shell script, you get a new environment, with only exported variables, unless you "source" it by preceeding the script command with a period (".") and a space, then your script name:



                  Unix>  . some_script.sh  # runs in current environment


                  Debugging tip: Include near the top of your script the set command to see what variables your script can see.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 17 at 21:00









                  Mark StewartMark Stewart

                  6181515




                  6181515






























                      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%2f495161%2fimport-environment-variables-in-a-bash-script%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

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith