import environment variables in a bash script












1















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
    1 hour ago
















1















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
    1 hour ago














1












1








1








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









Jeff Schaller

39.5k1054126




39.5k1054126










asked 3 hours ago









MarkMark

4852624




4852624













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

    – Julien Lopez
    1 hour ago



















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

    – Julien Lopez
    1 hour ago

















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

– Julien Lopez
1 hour ago





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

– Julien Lopez
1 hour ago










2 Answers
2






active

oldest

votes


















5














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









      5














      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






























        5














        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




























          5












          5








          5







          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 2 hours ago

























          answered 3 hours ago









          KusalanandaKusalananda

          124k16234386




          124k16234386

























              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 3 hours ago









                  Mark StewartMark Stewart

                  6101515




                  6101515






























                      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

                      Magento 2 controller redirect on button click in phtml file

                      Polycentropodidae