How can I add a directory to PATH in a script so that it affects the calling shell and the rest of the...












2














I am new to Ubuntu.



I wrote a script to add a dir to the PATH environment. When I run the script it runs fine and the dir is added to the PATH. But it seems that the change only lasts until the script exits instead of lasting for the length of the session. When I look at the PATH after the script is ran the dir is no longer there. Any suggestions?














share|improve this question
























  • There are no "Images below", only tags. Your question is clear and doesn't really need any screenshots to explain your situation further. However, FYI, information on inserting images into posts can be found at: meta.stackexchange.com/questions/75491/…
    – CentaurusA
    Sep 9 at 21:22






  • 2




    Please don't post photos of text. Copy the text here and apply code formatting: askubuntu.com/editing-help#code
    – muru
    Sep 10 at 8:30
















2














I am new to Ubuntu.



I wrote a script to add a dir to the PATH environment. When I run the script it runs fine and the dir is added to the PATH. But it seems that the change only lasts until the script exits instead of lasting for the length of the session. When I look at the PATH after the script is ran the dir is no longer there. Any suggestions?














share|improve this question
























  • There are no "Images below", only tags. Your question is clear and doesn't really need any screenshots to explain your situation further. However, FYI, information on inserting images into posts can be found at: meta.stackexchange.com/questions/75491/…
    – CentaurusA
    Sep 9 at 21:22






  • 2




    Please don't post photos of text. Copy the text here and apply code formatting: askubuntu.com/editing-help#code
    – muru
    Sep 10 at 8:30














2












2








2







I am new to Ubuntu.



I wrote a script to add a dir to the PATH environment. When I run the script it runs fine and the dir is added to the PATH. But it seems that the change only lasts until the script exits instead of lasting for the length of the session. When I look at the PATH after the script is ran the dir is no longer there. Any suggestions?














share|improve this question















I am new to Ubuntu.



I wrote a script to add a dir to the PATH environment. When I run the script it runs fine and the dir is added to the PATH. But it seems that the change only lasts until the script exits instead of lasting for the length of the session. When I look at the PATH after the script is ran the dir is no longer there. Any suggestions?











command-line bash scripts environment-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 10 at 7:35









Zanna

50k13131238




50k13131238










asked Sep 9 at 20:51









JP JP

134




134












  • There are no "Images below", only tags. Your question is clear and doesn't really need any screenshots to explain your situation further. However, FYI, information on inserting images into posts can be found at: meta.stackexchange.com/questions/75491/…
    – CentaurusA
    Sep 9 at 21:22






  • 2




    Please don't post photos of text. Copy the text here and apply code formatting: askubuntu.com/editing-help#code
    – muru
    Sep 10 at 8:30


















  • There are no "Images below", only tags. Your question is clear and doesn't really need any screenshots to explain your situation further. However, FYI, information on inserting images into posts can be found at: meta.stackexchange.com/questions/75491/…
    – CentaurusA
    Sep 9 at 21:22






  • 2




    Please don't post photos of text. Copy the text here and apply code formatting: askubuntu.com/editing-help#code
    – muru
    Sep 10 at 8:30
















There are no "Images below", only tags. Your question is clear and doesn't really need any screenshots to explain your situation further. However, FYI, information on inserting images into posts can be found at: meta.stackexchange.com/questions/75491/…
– CentaurusA
Sep 9 at 21:22




There are no "Images below", only tags. Your question is clear and doesn't really need any screenshots to explain your situation further. However, FYI, information on inserting images into posts can be found at: meta.stackexchange.com/questions/75491/…
– CentaurusA
Sep 9 at 21:22




2




2




Please don't post photos of text. Copy the text here and apply code formatting: askubuntu.com/editing-help#code
– muru
Sep 10 at 8:30




Please don't post photos of text. Copy the text here and apply code formatting: askubuntu.com/editing-help#code
– muru
Sep 10 at 8:30










2 Answers
2






active

oldest

votes


















2














There's two things to remember:




  1. Commands, including scripts, keep their environment for the duration of the command running


  2. Commands inherit environment from parent process. For commands started via shell, they'll inherit from the shell.



So if you do PATH=$PATH:/my/dir that'll last only for the scripts duration. To make it permanent, the parent shell needs to be aware of the change. Proper way to do that would be to write to ~/.bashrc if you're using bash or appropriate rc file for your shell. Thus we can use >> to append to file



echo PATH=$PATH:/my/dir >> ~/.bashrc


And when the script exits, run



source ~/.bashrc


so that the shell rereads the config and will be aware of the changes. Now every command you run in shell and every new interactive shell started will inherit new PATH variable



The two steps can be put together into a function since ( at least for bash ) functions run in current shell environment, so unlike a script when you do source part, calling source from a function will affect the current shell.






share|improve this answer































    1














    Sergiy Kolodyazhnyy's answer identifies the problem here: scripts are executed in a separate child shell of the shell that calls them, and commands that affect the shell itself, such as assigning to variables or changing working directory, do not affect the calling shell at all, only the child shell and (if they export variables to the environment) its children.



    You can play with this using humble shell variables...



    $ foo=bar
    $ echo $foo
    bar
    $ echo -e "foo=baz n"'echo $foo' > script
    $ cat script
    foo=baz
    echo $foo
    $ bash script
    baz
    $ echo $foo
    bar


    Although as Eliah Kagan shows in this answer it's easier to do so with subshells.



    I am writing this answer in case you do not want to permanently add the directory to your PATH, but only to the current shell session.



    To do this you simply need to run the script in the current shell. This is done with the source command which is abbreviated to . (dot).



    Given this slightly simplified version of your script...



    read -rp "What did you want to add to PATH? "
    [ -d "$REPLY" ] &&
    PATH="$PATH:$(readlink -m $REPLY)" &&
    echo "OK, adding $REPLY to PATH" &&
    echo "$PATH" ||
    echo "seems like $REPLY is not a directory"


    Notice that I get the same result as you when I run the script in the usual way:



    $ ./add-to-path
    What did you want to add to PATH? /home/zanna/playground
    OK, adding /home/zanna/playground to PATH
    /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
    $ echo $PATH
    /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games


    But when I source the script it works as expected:



    $ . add-to-path
    What did you want to add to PATH? /home/zanna/playground
    OK, adding /home/zanna/playground to PATH
    /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
    $ echo $PATH
    /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground


    I will add three asides:




    • I recommend adding PATH assignments to ~/.profile rather than ~/.bashrc because ~/.bashrc is sourced by every interactive Bash shell, including shells started from the current shell - this means child shells could end up with really long PATHs, as they inherit PATH as well as appending to it when they source ~/.bashrc. In contrast, ~/.profile is usually only sourced at login (or by login shells).

    • You don't need to export when you assign to PATH because it's already an environment variable: in a sense it is already exported and will remain so: an assignment to PATH will always be inherited by child processes (though not of parent processes, as you discovered!) without being explicitly exported.


    • I have quoted the variables REPLY and PATH throughout. This is a good idea because either may have spaces or other characters that trigger shell expansions. However, a side effect of this is that ~ is not expanded, so the script is apt to return things like



      looks like ~/some-existing-dir is not a directory


      which is true (taking ~ literally) but not very helpful. Maybe the script should warn the user of this...








    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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%2faskubuntu.com%2fquestions%2f1073760%2fhow-can-i-add-a-directory-to-path-in-a-script-so-that-it-affects-the-calling-she%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









      2














      There's two things to remember:




      1. Commands, including scripts, keep their environment for the duration of the command running


      2. Commands inherit environment from parent process. For commands started via shell, they'll inherit from the shell.



      So if you do PATH=$PATH:/my/dir that'll last only for the scripts duration. To make it permanent, the parent shell needs to be aware of the change. Proper way to do that would be to write to ~/.bashrc if you're using bash or appropriate rc file for your shell. Thus we can use >> to append to file



      echo PATH=$PATH:/my/dir >> ~/.bashrc


      And when the script exits, run



      source ~/.bashrc


      so that the shell rereads the config and will be aware of the changes. Now every command you run in shell and every new interactive shell started will inherit new PATH variable



      The two steps can be put together into a function since ( at least for bash ) functions run in current shell environment, so unlike a script when you do source part, calling source from a function will affect the current shell.






      share|improve this answer




























        2














        There's two things to remember:




        1. Commands, including scripts, keep their environment for the duration of the command running


        2. Commands inherit environment from parent process. For commands started via shell, they'll inherit from the shell.



        So if you do PATH=$PATH:/my/dir that'll last only for the scripts duration. To make it permanent, the parent shell needs to be aware of the change. Proper way to do that would be to write to ~/.bashrc if you're using bash or appropriate rc file for your shell. Thus we can use >> to append to file



        echo PATH=$PATH:/my/dir >> ~/.bashrc


        And when the script exits, run



        source ~/.bashrc


        so that the shell rereads the config and will be aware of the changes. Now every command you run in shell and every new interactive shell started will inherit new PATH variable



        The two steps can be put together into a function since ( at least for bash ) functions run in current shell environment, so unlike a script when you do source part, calling source from a function will affect the current shell.






        share|improve this answer


























          2












          2








          2






          There's two things to remember:




          1. Commands, including scripts, keep their environment for the duration of the command running


          2. Commands inherit environment from parent process. For commands started via shell, they'll inherit from the shell.



          So if you do PATH=$PATH:/my/dir that'll last only for the scripts duration. To make it permanent, the parent shell needs to be aware of the change. Proper way to do that would be to write to ~/.bashrc if you're using bash or appropriate rc file for your shell. Thus we can use >> to append to file



          echo PATH=$PATH:/my/dir >> ~/.bashrc


          And when the script exits, run



          source ~/.bashrc


          so that the shell rereads the config and will be aware of the changes. Now every command you run in shell and every new interactive shell started will inherit new PATH variable



          The two steps can be put together into a function since ( at least for bash ) functions run in current shell environment, so unlike a script when you do source part, calling source from a function will affect the current shell.






          share|improve this answer














          There's two things to remember:




          1. Commands, including scripts, keep their environment for the duration of the command running


          2. Commands inherit environment from parent process. For commands started via shell, they'll inherit from the shell.



          So if you do PATH=$PATH:/my/dir that'll last only for the scripts duration. To make it permanent, the parent shell needs to be aware of the change. Proper way to do that would be to write to ~/.bashrc if you're using bash or appropriate rc file for your shell. Thus we can use >> to append to file



          echo PATH=$PATH:/my/dir >> ~/.bashrc


          And when the script exits, run



          source ~/.bashrc


          so that the shell rereads the config and will be aware of the changes. Now every command you run in shell and every new interactive shell started will inherit new PATH variable



          The two steps can be put together into a function since ( at least for bash ) functions run in current shell environment, so unlike a script when you do source part, calling source from a function will affect the current shell.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 9 at 21:09

























          answered Sep 9 at 20:58









          Sergiy Kolodyazhnyy

          69.3k9144304




          69.3k9144304

























              1














              Sergiy Kolodyazhnyy's answer identifies the problem here: scripts are executed in a separate child shell of the shell that calls them, and commands that affect the shell itself, such as assigning to variables or changing working directory, do not affect the calling shell at all, only the child shell and (if they export variables to the environment) its children.



              You can play with this using humble shell variables...



              $ foo=bar
              $ echo $foo
              bar
              $ echo -e "foo=baz n"'echo $foo' > script
              $ cat script
              foo=baz
              echo $foo
              $ bash script
              baz
              $ echo $foo
              bar


              Although as Eliah Kagan shows in this answer it's easier to do so with subshells.



              I am writing this answer in case you do not want to permanently add the directory to your PATH, but only to the current shell session.



              To do this you simply need to run the script in the current shell. This is done with the source command which is abbreviated to . (dot).



              Given this slightly simplified version of your script...



              read -rp "What did you want to add to PATH? "
              [ -d "$REPLY" ] &&
              PATH="$PATH:$(readlink -m $REPLY)" &&
              echo "OK, adding $REPLY to PATH" &&
              echo "$PATH" ||
              echo "seems like $REPLY is not a directory"


              Notice that I get the same result as you when I run the script in the usual way:



              $ ./add-to-path
              What did you want to add to PATH? /home/zanna/playground
              OK, adding /home/zanna/playground to PATH
              /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
              $ echo $PATH
              /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games


              But when I source the script it works as expected:



              $ . add-to-path
              What did you want to add to PATH? /home/zanna/playground
              OK, adding /home/zanna/playground to PATH
              /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
              $ echo $PATH
              /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground


              I will add three asides:




              • I recommend adding PATH assignments to ~/.profile rather than ~/.bashrc because ~/.bashrc is sourced by every interactive Bash shell, including shells started from the current shell - this means child shells could end up with really long PATHs, as they inherit PATH as well as appending to it when they source ~/.bashrc. In contrast, ~/.profile is usually only sourced at login (or by login shells).

              • You don't need to export when you assign to PATH because it's already an environment variable: in a sense it is already exported and will remain so: an assignment to PATH will always be inherited by child processes (though not of parent processes, as you discovered!) without being explicitly exported.


              • I have quoted the variables REPLY and PATH throughout. This is a good idea because either may have spaces or other characters that trigger shell expansions. However, a side effect of this is that ~ is not expanded, so the script is apt to return things like



                looks like ~/some-existing-dir is not a directory


                which is true (taking ~ literally) but not very helpful. Maybe the script should warn the user of this...








              share|improve this answer




























                1














                Sergiy Kolodyazhnyy's answer identifies the problem here: scripts are executed in a separate child shell of the shell that calls them, and commands that affect the shell itself, such as assigning to variables or changing working directory, do not affect the calling shell at all, only the child shell and (if they export variables to the environment) its children.



                You can play with this using humble shell variables...



                $ foo=bar
                $ echo $foo
                bar
                $ echo -e "foo=baz n"'echo $foo' > script
                $ cat script
                foo=baz
                echo $foo
                $ bash script
                baz
                $ echo $foo
                bar


                Although as Eliah Kagan shows in this answer it's easier to do so with subshells.



                I am writing this answer in case you do not want to permanently add the directory to your PATH, but only to the current shell session.



                To do this you simply need to run the script in the current shell. This is done with the source command which is abbreviated to . (dot).



                Given this slightly simplified version of your script...



                read -rp "What did you want to add to PATH? "
                [ -d "$REPLY" ] &&
                PATH="$PATH:$(readlink -m $REPLY)" &&
                echo "OK, adding $REPLY to PATH" &&
                echo "$PATH" ||
                echo "seems like $REPLY is not a directory"


                Notice that I get the same result as you when I run the script in the usual way:



                $ ./add-to-path
                What did you want to add to PATH? /home/zanna/playground
                OK, adding /home/zanna/playground to PATH
                /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
                $ echo $PATH
                /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games


                But when I source the script it works as expected:



                $ . add-to-path
                What did you want to add to PATH? /home/zanna/playground
                OK, adding /home/zanna/playground to PATH
                /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
                $ echo $PATH
                /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground


                I will add three asides:




                • I recommend adding PATH assignments to ~/.profile rather than ~/.bashrc because ~/.bashrc is sourced by every interactive Bash shell, including shells started from the current shell - this means child shells could end up with really long PATHs, as they inherit PATH as well as appending to it when they source ~/.bashrc. In contrast, ~/.profile is usually only sourced at login (or by login shells).

                • You don't need to export when you assign to PATH because it's already an environment variable: in a sense it is already exported and will remain so: an assignment to PATH will always be inherited by child processes (though not of parent processes, as you discovered!) without being explicitly exported.


                • I have quoted the variables REPLY and PATH throughout. This is a good idea because either may have spaces or other characters that trigger shell expansions. However, a side effect of this is that ~ is not expanded, so the script is apt to return things like



                  looks like ~/some-existing-dir is not a directory


                  which is true (taking ~ literally) but not very helpful. Maybe the script should warn the user of this...








                share|improve this answer


























                  1












                  1








                  1






                  Sergiy Kolodyazhnyy's answer identifies the problem here: scripts are executed in a separate child shell of the shell that calls them, and commands that affect the shell itself, such as assigning to variables or changing working directory, do not affect the calling shell at all, only the child shell and (if they export variables to the environment) its children.



                  You can play with this using humble shell variables...



                  $ foo=bar
                  $ echo $foo
                  bar
                  $ echo -e "foo=baz n"'echo $foo' > script
                  $ cat script
                  foo=baz
                  echo $foo
                  $ bash script
                  baz
                  $ echo $foo
                  bar


                  Although as Eliah Kagan shows in this answer it's easier to do so with subshells.



                  I am writing this answer in case you do not want to permanently add the directory to your PATH, but only to the current shell session.



                  To do this you simply need to run the script in the current shell. This is done with the source command which is abbreviated to . (dot).



                  Given this slightly simplified version of your script...



                  read -rp "What did you want to add to PATH? "
                  [ -d "$REPLY" ] &&
                  PATH="$PATH:$(readlink -m $REPLY)" &&
                  echo "OK, adding $REPLY to PATH" &&
                  echo "$PATH" ||
                  echo "seems like $REPLY is not a directory"


                  Notice that I get the same result as you when I run the script in the usual way:



                  $ ./add-to-path
                  What did you want to add to PATH? /home/zanna/playground
                  OK, adding /home/zanna/playground to PATH
                  /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
                  $ echo $PATH
                  /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games


                  But when I source the script it works as expected:



                  $ . add-to-path
                  What did you want to add to PATH? /home/zanna/playground
                  OK, adding /home/zanna/playground to PATH
                  /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
                  $ echo $PATH
                  /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground


                  I will add three asides:




                  • I recommend adding PATH assignments to ~/.profile rather than ~/.bashrc because ~/.bashrc is sourced by every interactive Bash shell, including shells started from the current shell - this means child shells could end up with really long PATHs, as they inherit PATH as well as appending to it when they source ~/.bashrc. In contrast, ~/.profile is usually only sourced at login (or by login shells).

                  • You don't need to export when you assign to PATH because it's already an environment variable: in a sense it is already exported and will remain so: an assignment to PATH will always be inherited by child processes (though not of parent processes, as you discovered!) without being explicitly exported.


                  • I have quoted the variables REPLY and PATH throughout. This is a good idea because either may have spaces or other characters that trigger shell expansions. However, a side effect of this is that ~ is not expanded, so the script is apt to return things like



                    looks like ~/some-existing-dir is not a directory


                    which is true (taking ~ literally) but not very helpful. Maybe the script should warn the user of this...








                  share|improve this answer














                  Sergiy Kolodyazhnyy's answer identifies the problem here: scripts are executed in a separate child shell of the shell that calls them, and commands that affect the shell itself, such as assigning to variables or changing working directory, do not affect the calling shell at all, only the child shell and (if they export variables to the environment) its children.



                  You can play with this using humble shell variables...



                  $ foo=bar
                  $ echo $foo
                  bar
                  $ echo -e "foo=baz n"'echo $foo' > script
                  $ cat script
                  foo=baz
                  echo $foo
                  $ bash script
                  baz
                  $ echo $foo
                  bar


                  Although as Eliah Kagan shows in this answer it's easier to do so with subshells.



                  I am writing this answer in case you do not want to permanently add the directory to your PATH, but only to the current shell session.



                  To do this you simply need to run the script in the current shell. This is done with the source command which is abbreviated to . (dot).



                  Given this slightly simplified version of your script...



                  read -rp "What did you want to add to PATH? "
                  [ -d "$REPLY" ] &&
                  PATH="$PATH:$(readlink -m $REPLY)" &&
                  echo "OK, adding $REPLY to PATH" &&
                  echo "$PATH" ||
                  echo "seems like $REPLY is not a directory"


                  Notice that I get the same result as you when I run the script in the usual way:



                  $ ./add-to-path
                  What did you want to add to PATH? /home/zanna/playground
                  OK, adding /home/zanna/playground to PATH
                  /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
                  $ echo $PATH
                  /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games


                  But when I source the script it works as expected:



                  $ . add-to-path
                  What did you want to add to PATH? /home/zanna/playground
                  OK, adding /home/zanna/playground to PATH
                  /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground
                  $ echo $PATH
                  /home/zanna/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/zanna/playground


                  I will add three asides:




                  • I recommend adding PATH assignments to ~/.profile rather than ~/.bashrc because ~/.bashrc is sourced by every interactive Bash shell, including shells started from the current shell - this means child shells could end up with really long PATHs, as they inherit PATH as well as appending to it when they source ~/.bashrc. In contrast, ~/.profile is usually only sourced at login (or by login shells).

                  • You don't need to export when you assign to PATH because it's already an environment variable: in a sense it is already exported and will remain so: an assignment to PATH will always be inherited by child processes (though not of parent processes, as you discovered!) without being explicitly exported.


                  • I have quoted the variables REPLY and PATH throughout. This is a good idea because either may have spaces or other characters that trigger shell expansions. However, a side effect of this is that ~ is not expanded, so the script is apt to return things like



                    looks like ~/some-existing-dir is not a directory


                    which is true (taking ~ literally) but not very helpful. Maybe the script should warn the user of this...









                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 10 at 8:26

























                  answered Sep 10 at 8:15









                  Zanna

                  50k13131238




                  50k13131238






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • 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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2faskubuntu.com%2fquestions%2f1073760%2fhow-can-i-add-a-directory-to-path-in-a-script-so-that-it-affects-the-calling-she%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

                      How did Captain America manage to do this?

                      迪纳利

                      南乌拉尔铁路局