keep environment when executing in script












1














I have a bash script that should be executing a number of commands. The commands work fine when entered in a termainal but when I try to execute them from inside the script they don't work, so I assume it's something environment-specific.



In the script I have



exec "$cmd"


and it doesn't work, while if I copy the contents of $cmd and paste in a terminal it works fine.



How do I keep the current environment when running the script? Or do you have any other idea what it might be?










share|improve this question




















  • 1




    Running the script does not change the environment by default. You've misdiagnosed the problem.
    – Ignacio Vazquez-Abrams
    Aug 22 '12 at 11:29








  • 1




    Be sure to export the variables needed by the script before running it.
    – choroba
    Aug 22 '12 at 11:31










  • Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
    – dutt
    Aug 22 '12 at 11:36










  • Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
    – terdon
    Aug 22 '12 at 12:56










  • I tried both and neither worked, is there any difference?
    – dutt
    Aug 22 '12 at 13:41
















1














I have a bash script that should be executing a number of commands. The commands work fine when entered in a termainal but when I try to execute them from inside the script they don't work, so I assume it's something environment-specific.



In the script I have



exec "$cmd"


and it doesn't work, while if I copy the contents of $cmd and paste in a terminal it works fine.



How do I keep the current environment when running the script? Or do you have any other idea what it might be?










share|improve this question




















  • 1




    Running the script does not change the environment by default. You've misdiagnosed the problem.
    – Ignacio Vazquez-Abrams
    Aug 22 '12 at 11:29








  • 1




    Be sure to export the variables needed by the script before running it.
    – choroba
    Aug 22 '12 at 11:31










  • Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
    – dutt
    Aug 22 '12 at 11:36










  • Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
    – terdon
    Aug 22 '12 at 12:56










  • I tried both and neither worked, is there any difference?
    – dutt
    Aug 22 '12 at 13:41














1












1








1







I have a bash script that should be executing a number of commands. The commands work fine when entered in a termainal but when I try to execute them from inside the script they don't work, so I assume it's something environment-specific.



In the script I have



exec "$cmd"


and it doesn't work, while if I copy the contents of $cmd and paste in a terminal it works fine.



How do I keep the current environment when running the script? Or do you have any other idea what it might be?










share|improve this question















I have a bash script that should be executing a number of commands. The commands work fine when entered in a termainal but when I try to execute them from inside the script they don't work, so I assume it's something environment-specific.



In the script I have



exec "$cmd"


and it doesn't work, while if I copy the contents of $cmd and paste in a terminal it works fine.



How do I keep the current environment when running the script? Or do you have any other idea what it might be?







bash bash-scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 22 '12 at 11:37







dutt

















asked Aug 22 '12 at 11:25









duttdutt

10614




10614








  • 1




    Running the script does not change the environment by default. You've misdiagnosed the problem.
    – Ignacio Vazquez-Abrams
    Aug 22 '12 at 11:29








  • 1




    Be sure to export the variables needed by the script before running it.
    – choroba
    Aug 22 '12 at 11:31










  • Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
    – dutt
    Aug 22 '12 at 11:36










  • Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
    – terdon
    Aug 22 '12 at 12:56










  • I tried both and neither worked, is there any difference?
    – dutt
    Aug 22 '12 at 13:41














  • 1




    Running the script does not change the environment by default. You've misdiagnosed the problem.
    – Ignacio Vazquez-Abrams
    Aug 22 '12 at 11:29








  • 1




    Be sure to export the variables needed by the script before running it.
    – choroba
    Aug 22 '12 at 11:31










  • Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
    – dutt
    Aug 22 '12 at 11:36










  • Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
    – terdon
    Aug 22 '12 at 12:56










  • I tried both and neither worked, is there any difference?
    – dutt
    Aug 22 '12 at 13:41








1




1




Running the script does not change the environment by default. You've misdiagnosed the problem.
– Ignacio Vazquez-Abrams
Aug 22 '12 at 11:29






Running the script does not change the environment by default. You've misdiagnosed the problem.
– Ignacio Vazquez-Abrams
Aug 22 '12 at 11:29






1




1




Be sure to export the variables needed by the script before running it.
– choroba
Aug 22 '12 at 11:31




Be sure to export the variables needed by the script before running it.
– choroba
Aug 22 '12 at 11:31












Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
– dutt
Aug 22 '12 at 11:36




Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
– dutt
Aug 22 '12 at 11:36












Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
– terdon
Aug 22 '12 at 12:56




Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
– terdon
Aug 22 '12 at 12:56












I tried both and neither worked, is there any difference?
– dutt
Aug 22 '12 at 13:41




I tried both and neither worked, is there any difference?
– dutt
Aug 22 '12 at 13:41










3 Answers
3






active

oldest

votes


















0














Put the command in an array instead.



arr=(foo bar baz quux)
exec "${arr[@]}"





share|improve this answer





























    0














    Type set -o allexport at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).






    share|improve this answer





























      -2














      If all the command should be ran from the same dir you can use



      cd /d $Location


      at the beginning of the script to set the location for the commands to be ran.



      cd -- This is the change directory command.
      /d -- This switch makes cd change both drive and directory at once.



      if you wanted the command to be ran from say D:scripts
      with out the /d you would need to



      D:
      cd scripts


      but with the above command you can do it in one line



      cd /d D:scripts


      -Edit again-



      My apologies, it clearly says BASH but i read it at BATCH for some reason.
      will be more careful in the future.






      share|improve this answer



















      • 1




        Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what /d is supposed to mean? Aren't you confusing cd of cmd.exe with the one of bash? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
        – Kamil Maciorowski
        Dec 18 '18 at 6:34










      • @KamilMaciorowski - edited to explain more.
        – jesse
        Dec 18 '18 at 13:29






      • 1




        The question is tagged bash but your answer explains cd of cmd.exe. cd in Bash will treat /d as a path. On the other hand $Location looks like a Bash variable and I guess cmd.exe won't understand it. My downvote stands because the answer confuses two different cd commands and it's therefore not useful.
        – Kamil Maciorowski
        Dec 18 '18 at 13:37












      • One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to cd of cmd.exe and keep what refers to cd of bash (not the other way around).
        – Kamil Maciorowski
        Dec 18 '18 at 13:49











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fsuperuser.com%2fquestions%2f464811%2fkeep-environment-when-executing-in-script%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









      0














      Put the command in an array instead.



      arr=(foo bar baz quux)
      exec "${arr[@]}"





      share|improve this answer


























        0














        Put the command in an array instead.



        arr=(foo bar baz quux)
        exec "${arr[@]}"





        share|improve this answer
























          0












          0








          0






          Put the command in an array instead.



          arr=(foo bar baz quux)
          exec "${arr[@]}"





          share|improve this answer












          Put the command in an array instead.



          arr=(foo bar baz quux)
          exec "${arr[@]}"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 22 '12 at 11:40









          Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams

          95.6k6150209




          95.6k6150209

























              0














              Type set -o allexport at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).






              share|improve this answer


























                0














                Type set -o allexport at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).






                share|improve this answer
























                  0












                  0








                  0






                  Type set -o allexport at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).






                  share|improve this answer












                  Type set -o allexport at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 23 '12 at 19:32









                  srcsrc

                  1816




                  1816























                      -2














                      If all the command should be ran from the same dir you can use



                      cd /d $Location


                      at the beginning of the script to set the location for the commands to be ran.



                      cd -- This is the change directory command.
                      /d -- This switch makes cd change both drive and directory at once.



                      if you wanted the command to be ran from say D:scripts
                      with out the /d you would need to



                      D:
                      cd scripts


                      but with the above command you can do it in one line



                      cd /d D:scripts


                      -Edit again-



                      My apologies, it clearly says BASH but i read it at BATCH for some reason.
                      will be more careful in the future.






                      share|improve this answer



















                      • 1




                        Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what /d is supposed to mean? Aren't you confusing cd of cmd.exe with the one of bash? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
                        – Kamil Maciorowski
                        Dec 18 '18 at 6:34










                      • @KamilMaciorowski - edited to explain more.
                        – jesse
                        Dec 18 '18 at 13:29






                      • 1




                        The question is tagged bash but your answer explains cd of cmd.exe. cd in Bash will treat /d as a path. On the other hand $Location looks like a Bash variable and I guess cmd.exe won't understand it. My downvote stands because the answer confuses two different cd commands and it's therefore not useful.
                        – Kamil Maciorowski
                        Dec 18 '18 at 13:37












                      • One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to cd of cmd.exe and keep what refers to cd of bash (not the other way around).
                        – Kamil Maciorowski
                        Dec 18 '18 at 13:49
















                      -2














                      If all the command should be ran from the same dir you can use



                      cd /d $Location


                      at the beginning of the script to set the location for the commands to be ran.



                      cd -- This is the change directory command.
                      /d -- This switch makes cd change both drive and directory at once.



                      if you wanted the command to be ran from say D:scripts
                      with out the /d you would need to



                      D:
                      cd scripts


                      but with the above command you can do it in one line



                      cd /d D:scripts


                      -Edit again-



                      My apologies, it clearly says BASH but i read it at BATCH for some reason.
                      will be more careful in the future.






                      share|improve this answer



















                      • 1




                        Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what /d is supposed to mean? Aren't you confusing cd of cmd.exe with the one of bash? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
                        – Kamil Maciorowski
                        Dec 18 '18 at 6:34










                      • @KamilMaciorowski - edited to explain more.
                        – jesse
                        Dec 18 '18 at 13:29






                      • 1




                        The question is tagged bash but your answer explains cd of cmd.exe. cd in Bash will treat /d as a path. On the other hand $Location looks like a Bash variable and I guess cmd.exe won't understand it. My downvote stands because the answer confuses two different cd commands and it's therefore not useful.
                        – Kamil Maciorowski
                        Dec 18 '18 at 13:37












                      • One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to cd of cmd.exe and keep what refers to cd of bash (not the other way around).
                        – Kamil Maciorowski
                        Dec 18 '18 at 13:49














                      -2












                      -2








                      -2






                      If all the command should be ran from the same dir you can use



                      cd /d $Location


                      at the beginning of the script to set the location for the commands to be ran.



                      cd -- This is the change directory command.
                      /d -- This switch makes cd change both drive and directory at once.



                      if you wanted the command to be ran from say D:scripts
                      with out the /d you would need to



                      D:
                      cd scripts


                      but with the above command you can do it in one line



                      cd /d D:scripts


                      -Edit again-



                      My apologies, it clearly says BASH but i read it at BATCH for some reason.
                      will be more careful in the future.






                      share|improve this answer














                      If all the command should be ran from the same dir you can use



                      cd /d $Location


                      at the beginning of the script to set the location for the commands to be ran.



                      cd -- This is the change directory command.
                      /d -- This switch makes cd change both drive and directory at once.



                      if you wanted the command to be ran from say D:scripts
                      with out the /d you would need to



                      D:
                      cd scripts


                      but with the above command you can do it in one line



                      cd /d D:scripts


                      -Edit again-



                      My apologies, it clearly says BASH but i read it at BATCH for some reason.
                      will be more careful in the future.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 18 '18 at 14:03

























                      answered Dec 17 '18 at 20:53









                      jessejesse

                      1011




                      1011








                      • 1




                        Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what /d is supposed to mean? Aren't you confusing cd of cmd.exe with the one of bash? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
                        – Kamil Maciorowski
                        Dec 18 '18 at 6:34










                      • @KamilMaciorowski - edited to explain more.
                        – jesse
                        Dec 18 '18 at 13:29






                      • 1




                        The question is tagged bash but your answer explains cd of cmd.exe. cd in Bash will treat /d as a path. On the other hand $Location looks like a Bash variable and I guess cmd.exe won't understand it. My downvote stands because the answer confuses two different cd commands and it's therefore not useful.
                        – Kamil Maciorowski
                        Dec 18 '18 at 13:37












                      • One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to cd of cmd.exe and keep what refers to cd of bash (not the other way around).
                        – Kamil Maciorowski
                        Dec 18 '18 at 13:49














                      • 1




                        Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what /d is supposed to mean? Aren't you confusing cd of cmd.exe with the one of bash? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
                        – Kamil Maciorowski
                        Dec 18 '18 at 6:34










                      • @KamilMaciorowski - edited to explain more.
                        – jesse
                        Dec 18 '18 at 13:29






                      • 1




                        The question is tagged bash but your answer explains cd of cmd.exe. cd in Bash will treat /d as a path. On the other hand $Location looks like a Bash variable and I guess cmd.exe won't understand it. My downvote stands because the answer confuses two different cd commands and it's therefore not useful.
                        – Kamil Maciorowski
                        Dec 18 '18 at 13:37












                      • One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to cd of cmd.exe and keep what refers to cd of bash (not the other way around).
                        – Kamil Maciorowski
                        Dec 18 '18 at 13:49








                      1




                      1




                      Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what /d is supposed to mean? Aren't you confusing cd of cmd.exe with the one of bash? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
                      – Kamil Maciorowski
                      Dec 18 '18 at 6:34




                      Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what /d is supposed to mean? Aren't you confusing cd of cmd.exe with the one of bash? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
                      – Kamil Maciorowski
                      Dec 18 '18 at 6:34












                      @KamilMaciorowski - edited to explain more.
                      – jesse
                      Dec 18 '18 at 13:29




                      @KamilMaciorowski - edited to explain more.
                      – jesse
                      Dec 18 '18 at 13:29




                      1




                      1




                      The question is tagged bash but your answer explains cd of cmd.exe. cd in Bash will treat /d as a path. On the other hand $Location looks like a Bash variable and I guess cmd.exe won't understand it. My downvote stands because the answer confuses two different cd commands and it's therefore not useful.
                      – Kamil Maciorowski
                      Dec 18 '18 at 13:37






                      The question is tagged bash but your answer explains cd of cmd.exe. cd in Bash will treat /d as a path. On the other hand $Location looks like a Bash variable and I guess cmd.exe won't understand it. My downvote stands because the answer confuses two different cd commands and it's therefore not useful.
                      – Kamil Maciorowski
                      Dec 18 '18 at 13:37














                      One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to cd of cmd.exe and keep what refers to cd of bash (not the other way around).
                      – Kamil Maciorowski
                      Dec 18 '18 at 13:49




                      One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to cd of cmd.exe and keep what refers to cd of bash (not the other way around).
                      – Kamil Maciorowski
                      Dec 18 '18 at 13:49


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • 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%2fsuperuser.com%2fquestions%2f464811%2fkeep-environment-when-executing-in-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

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy