Script to change current directory (cd, pwd)












33















I want to run a script to simply change the current working directory:



#!/bin/bash
cd web/www/project


But, after I run it, the current pwd remains unchanged! How can I do that?










share|improve this question

























  • Can your provide more information? Some thing like your directory structure, or the context...

    – favadi
    Dec 19 '11 at 8:06






  • 2





    This is a rite of passage problem

    – zzapper
    Aug 25 '16 at 11:41






  • 1





    Heh. You cracked me up, @zzapper

    – SDsolar
    Jul 24 '17 at 15:16
















33















I want to run a script to simply change the current working directory:



#!/bin/bash
cd web/www/project


But, after I run it, the current pwd remains unchanged! How can I do that?










share|improve this question

























  • Can your provide more information? Some thing like your directory structure, or the context...

    – favadi
    Dec 19 '11 at 8:06






  • 2





    This is a rite of passage problem

    – zzapper
    Aug 25 '16 at 11:41






  • 1





    Heh. You cracked me up, @zzapper

    – SDsolar
    Jul 24 '17 at 15:16














33












33








33


9






I want to run a script to simply change the current working directory:



#!/bin/bash
cd web/www/project


But, after I run it, the current pwd remains unchanged! How can I do that?










share|improve this question
















I want to run a script to simply change the current working directory:



#!/bin/bash
cd web/www/project


But, after I run it, the current pwd remains unchanged! How can I do that?







bash scripting environment-variables cd-command






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '11 at 20:44









Sachin Divekar

3,92811719




3,92811719










asked Dec 19 '11 at 7:55









Sony SantosSony Santos

268137




268137













  • Can your provide more information? Some thing like your directory structure, or the context...

    – favadi
    Dec 19 '11 at 8:06






  • 2





    This is a rite of passage problem

    – zzapper
    Aug 25 '16 at 11:41






  • 1





    Heh. You cracked me up, @zzapper

    – SDsolar
    Jul 24 '17 at 15:16



















  • Can your provide more information? Some thing like your directory structure, or the context...

    – favadi
    Dec 19 '11 at 8:06






  • 2





    This is a rite of passage problem

    – zzapper
    Aug 25 '16 at 11:41






  • 1





    Heh. You cracked me up, @zzapper

    – SDsolar
    Jul 24 '17 at 15:16

















Can your provide more information? Some thing like your directory structure, or the context...

– favadi
Dec 19 '11 at 8:06





Can your provide more information? Some thing like your directory structure, or the context...

– favadi
Dec 19 '11 at 8:06




2




2





This is a rite of passage problem

– zzapper
Aug 25 '16 at 11:41





This is a rite of passage problem

– zzapper
Aug 25 '16 at 11:41




1




1





Heh. You cracked me up, @zzapper

– SDsolar
Jul 24 '17 at 15:16





Heh. You cracked me up, @zzapper

– SDsolar
Jul 24 '17 at 15:16










6 Answers
6






active

oldest

votes


















49














It is an expected behavior, and already discussed several times.



The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes.



To change directory permanently you should source the script, as in



. ./script





share|improve this answer





















  • 8





    @Sony: Note that you should use return to escape from a script sourced in this way, not exit - they are like shell functions, and exit will exit the shell that sourced the script.

    – Charles Stewart
    Dec 19 '11 at 8:19











  • @CharlesStewart In fact, I'm not familiar with sourced scripts. Thank you!

    – Sony Santos
    Dec 19 '11 at 12:56






  • 5





    is source ./script the same?

    – amyassin
    Dec 19 '11 at 13:04






  • 2





    @amyassin: yes, it is

    – enzotib
    Dec 19 '11 at 13:05






  • 2





    1. . and source are equal in bash. 2. we don't need to use ./ before filename if it's in the same directory. It is ok to run only this: . script

    – sobi3ch
    Jun 16 '16 at 15:02





















19














For small tasks such as this, instead of creating script, create an alias like this,



$ alias cdproj='cd /dir/web/www/proj'


You should add this to your .bashrc file, if you want it set for every interactive shell.



Now you can run this as $ cdproj.






share|improve this answer


























  • That's a good tip! I'll consider to use it for some tasks. Thank you! :)

    – Sony Santos
    Dec 19 '11 at 20:26






  • 1





    You can also have the script echo the commands to be executed, and then use eval `./script` or eval $(./script) to execute those commands. This is a common approach for commands that need to update the invoking shell's environment.

    – Keith Thompson
    Dec 20 '11 at 10:41








  • 2





    Just be very careful about what you output if you are going to go the eval approach.

    – jw013
    Sep 14 '12 at 20:08



















8














While there are answers that do the exact action that you want, a more standard method for such purpose is to create symbolic link:



ln -s ~/web/www/project proj   #use full path to dir!


Then you could cd to the directory using the name proj:



cd proj


This method is more flexible because you could access files using the short name without cd:



ls proj/   #note the endslash!
vim proj/file.x





share|improve this answer

































    7














    Use exec bash at the end




    A bash script operates on its current environment or on that of its
    children, but never on its parent environment.




    However, this question often gets asked because one wants to be left at the bash prompt in a certain directory after the execution of a bash script from another directory.



    If this is the case, simply execute a child bash instance at the end of the script:



    #!/bin/bash
    cd desired/directory
    exec bash





    share|improve this answer


























    • You are a genius!

      – Kasper
      Aug 15 '16 at 3:05











    • Better to just source the script, as in accepted answer: using exec is typically considered the last resort of a scoundrel.. :)

      – neuronet
      Aug 16 '16 at 0:18











    • this trick doesn't work in debian 9 stretch.

      – vdegenne
      May 5 '18 at 0:41













    • This is the wrong way to go about this!

      – Dennis Williamson
      4 hours ago



















    3














    If you change between directories far away in the filesystem. I will recommend autojump.






    share|improve this answer































      2














      Depends on what you're going to do, another solution can be creating a function instead of a script.



      Example:



      Create a function in a file, let's say /home/aidin/my-cd-script:



      function my-cd() {
      cd /to/my/path
      }


      Then include it in your bashrc or zshrc file:



      # Somewhere in rc file
      source /home/aidin/my-cd-script


      Now you can use it like a command:



      $ my-cd





      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%2f27139%2fscript-to-change-current-directory-cd-pwd%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        49














        It is an expected behavior, and already discussed several times.



        The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes.



        To change directory permanently you should source the script, as in



        . ./script





        share|improve this answer





















        • 8





          @Sony: Note that you should use return to escape from a script sourced in this way, not exit - they are like shell functions, and exit will exit the shell that sourced the script.

          – Charles Stewart
          Dec 19 '11 at 8:19











        • @CharlesStewart In fact, I'm not familiar with sourced scripts. Thank you!

          – Sony Santos
          Dec 19 '11 at 12:56






        • 5





          is source ./script the same?

          – amyassin
          Dec 19 '11 at 13:04






        • 2





          @amyassin: yes, it is

          – enzotib
          Dec 19 '11 at 13:05






        • 2





          1. . and source are equal in bash. 2. we don't need to use ./ before filename if it's in the same directory. It is ok to run only this: . script

          – sobi3ch
          Jun 16 '16 at 15:02


















        49














        It is an expected behavior, and already discussed several times.



        The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes.



        To change directory permanently you should source the script, as in



        . ./script





        share|improve this answer





















        • 8





          @Sony: Note that you should use return to escape from a script sourced in this way, not exit - they are like shell functions, and exit will exit the shell that sourced the script.

          – Charles Stewart
          Dec 19 '11 at 8:19











        • @CharlesStewart In fact, I'm not familiar with sourced scripts. Thank you!

          – Sony Santos
          Dec 19 '11 at 12:56






        • 5





          is source ./script the same?

          – amyassin
          Dec 19 '11 at 13:04






        • 2





          @amyassin: yes, it is

          – enzotib
          Dec 19 '11 at 13:05






        • 2





          1. . and source are equal in bash. 2. we don't need to use ./ before filename if it's in the same directory. It is ok to run only this: . script

          – sobi3ch
          Jun 16 '16 at 15:02
















        49












        49








        49







        It is an expected behavior, and already discussed several times.



        The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes.



        To change directory permanently you should source the script, as in



        . ./script





        share|improve this answer















        It is an expected behavior, and already discussed several times.



        The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes.



        To change directory permanently you should source the script, as in



        . ./script






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 19 '11 at 15:31









        Tomasz Nurkiewicz

        1032




        1032










        answered Dec 19 '11 at 8:13









        enzotibenzotib

        34.4k710395




        34.4k710395








        • 8





          @Sony: Note that you should use return to escape from a script sourced in this way, not exit - they are like shell functions, and exit will exit the shell that sourced the script.

          – Charles Stewart
          Dec 19 '11 at 8:19











        • @CharlesStewart In fact, I'm not familiar with sourced scripts. Thank you!

          – Sony Santos
          Dec 19 '11 at 12:56






        • 5





          is source ./script the same?

          – amyassin
          Dec 19 '11 at 13:04






        • 2





          @amyassin: yes, it is

          – enzotib
          Dec 19 '11 at 13:05






        • 2





          1. . and source are equal in bash. 2. we don't need to use ./ before filename if it's in the same directory. It is ok to run only this: . script

          – sobi3ch
          Jun 16 '16 at 15:02
















        • 8





          @Sony: Note that you should use return to escape from a script sourced in this way, not exit - they are like shell functions, and exit will exit the shell that sourced the script.

          – Charles Stewart
          Dec 19 '11 at 8:19











        • @CharlesStewart In fact, I'm not familiar with sourced scripts. Thank you!

          – Sony Santos
          Dec 19 '11 at 12:56






        • 5





          is source ./script the same?

          – amyassin
          Dec 19 '11 at 13:04






        • 2





          @amyassin: yes, it is

          – enzotib
          Dec 19 '11 at 13:05






        • 2





          1. . and source are equal in bash. 2. we don't need to use ./ before filename if it's in the same directory. It is ok to run only this: . script

          – sobi3ch
          Jun 16 '16 at 15:02










        8




        8





        @Sony: Note that you should use return to escape from a script sourced in this way, not exit - they are like shell functions, and exit will exit the shell that sourced the script.

        – Charles Stewart
        Dec 19 '11 at 8:19





        @Sony: Note that you should use return to escape from a script sourced in this way, not exit - they are like shell functions, and exit will exit the shell that sourced the script.

        – Charles Stewart
        Dec 19 '11 at 8:19













        @CharlesStewart In fact, I'm not familiar with sourced scripts. Thank you!

        – Sony Santos
        Dec 19 '11 at 12:56





        @CharlesStewart In fact, I'm not familiar with sourced scripts. Thank you!

        – Sony Santos
        Dec 19 '11 at 12:56




        5




        5





        is source ./script the same?

        – amyassin
        Dec 19 '11 at 13:04





        is source ./script the same?

        – amyassin
        Dec 19 '11 at 13:04




        2




        2





        @amyassin: yes, it is

        – enzotib
        Dec 19 '11 at 13:05





        @amyassin: yes, it is

        – enzotib
        Dec 19 '11 at 13:05




        2




        2





        1. . and source are equal in bash. 2. we don't need to use ./ before filename if it's in the same directory. It is ok to run only this: . script

        – sobi3ch
        Jun 16 '16 at 15:02







        1. . and source are equal in bash. 2. we don't need to use ./ before filename if it's in the same directory. It is ok to run only this: . script

        – sobi3ch
        Jun 16 '16 at 15:02















        19














        For small tasks such as this, instead of creating script, create an alias like this,



        $ alias cdproj='cd /dir/web/www/proj'


        You should add this to your .bashrc file, if you want it set for every interactive shell.



        Now you can run this as $ cdproj.






        share|improve this answer


























        • That's a good tip! I'll consider to use it for some tasks. Thank you! :)

          – Sony Santos
          Dec 19 '11 at 20:26






        • 1





          You can also have the script echo the commands to be executed, and then use eval `./script` or eval $(./script) to execute those commands. This is a common approach for commands that need to update the invoking shell's environment.

          – Keith Thompson
          Dec 20 '11 at 10:41








        • 2





          Just be very careful about what you output if you are going to go the eval approach.

          – jw013
          Sep 14 '12 at 20:08
















        19














        For small tasks such as this, instead of creating script, create an alias like this,



        $ alias cdproj='cd /dir/web/www/proj'


        You should add this to your .bashrc file, if you want it set for every interactive shell.



        Now you can run this as $ cdproj.






        share|improve this answer


























        • That's a good tip! I'll consider to use it for some tasks. Thank you! :)

          – Sony Santos
          Dec 19 '11 at 20:26






        • 1





          You can also have the script echo the commands to be executed, and then use eval `./script` or eval $(./script) to execute those commands. This is a common approach for commands that need to update the invoking shell's environment.

          – Keith Thompson
          Dec 20 '11 at 10:41








        • 2





          Just be very careful about what you output if you are going to go the eval approach.

          – jw013
          Sep 14 '12 at 20:08














        19












        19








        19







        For small tasks such as this, instead of creating script, create an alias like this,



        $ alias cdproj='cd /dir/web/www/proj'


        You should add this to your .bashrc file, if you want it set for every interactive shell.



        Now you can run this as $ cdproj.






        share|improve this answer















        For small tasks such as this, instead of creating script, create an alias like this,



        $ alias cdproj='cd /dir/web/www/proj'


        You should add this to your .bashrc file, if you want it set for every interactive shell.



        Now you can run this as $ cdproj.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 19 '11 at 20:42

























        answered Dec 19 '11 at 14:12









        Sachin DivekarSachin Divekar

        3,92811719




        3,92811719













        • That's a good tip! I'll consider to use it for some tasks. Thank you! :)

          – Sony Santos
          Dec 19 '11 at 20:26






        • 1





          You can also have the script echo the commands to be executed, and then use eval `./script` or eval $(./script) to execute those commands. This is a common approach for commands that need to update the invoking shell's environment.

          – Keith Thompson
          Dec 20 '11 at 10:41








        • 2





          Just be very careful about what you output if you are going to go the eval approach.

          – jw013
          Sep 14 '12 at 20:08



















        • That's a good tip! I'll consider to use it for some tasks. Thank you! :)

          – Sony Santos
          Dec 19 '11 at 20:26






        • 1





          You can also have the script echo the commands to be executed, and then use eval `./script` or eval $(./script) to execute those commands. This is a common approach for commands that need to update the invoking shell's environment.

          – Keith Thompson
          Dec 20 '11 at 10:41








        • 2





          Just be very careful about what you output if you are going to go the eval approach.

          – jw013
          Sep 14 '12 at 20:08

















        That's a good tip! I'll consider to use it for some tasks. Thank you! :)

        – Sony Santos
        Dec 19 '11 at 20:26





        That's a good tip! I'll consider to use it for some tasks. Thank you! :)

        – Sony Santos
        Dec 19 '11 at 20:26




        1




        1





        You can also have the script echo the commands to be executed, and then use eval `./script` or eval $(./script) to execute those commands. This is a common approach for commands that need to update the invoking shell's environment.

        – Keith Thompson
        Dec 20 '11 at 10:41







        You can also have the script echo the commands to be executed, and then use eval `./script` or eval $(./script) to execute those commands. This is a common approach for commands that need to update the invoking shell's environment.

        – Keith Thompson
        Dec 20 '11 at 10:41






        2




        2





        Just be very careful about what you output if you are going to go the eval approach.

        – jw013
        Sep 14 '12 at 20:08





        Just be very careful about what you output if you are going to go the eval approach.

        – jw013
        Sep 14 '12 at 20:08











        8














        While there are answers that do the exact action that you want, a more standard method for such purpose is to create symbolic link:



        ln -s ~/web/www/project proj   #use full path to dir!


        Then you could cd to the directory using the name proj:



        cd proj


        This method is more flexible because you could access files using the short name without cd:



        ls proj/   #note the endslash!
        vim proj/file.x





        share|improve this answer






























          8














          While there are answers that do the exact action that you want, a more standard method for such purpose is to create symbolic link:



          ln -s ~/web/www/project proj   #use full path to dir!


          Then you could cd to the directory using the name proj:



          cd proj


          This method is more flexible because you could access files using the short name without cd:



          ls proj/   #note the endslash!
          vim proj/file.x





          share|improve this answer




























            8












            8








            8







            While there are answers that do the exact action that you want, a more standard method for such purpose is to create symbolic link:



            ln -s ~/web/www/project proj   #use full path to dir!


            Then you could cd to the directory using the name proj:



            cd proj


            This method is more flexible because you could access files using the short name without cd:



            ls proj/   #note the endslash!
            vim proj/file.x





            share|improve this answer















            While there are answers that do the exact action that you want, a more standard method for such purpose is to create symbolic link:



            ln -s ~/web/www/project proj   #use full path to dir!


            Then you could cd to the directory using the name proj:



            cd proj


            This method is more flexible because you could access files using the short name without cd:



            ls proj/   #note the endslash!
            vim proj/file.x






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 28 '12 at 18:20









            Kevin

            27.6k1065103




            27.6k1065103










            answered Dec 22 '11 at 14:25









            corvinuscorvinus

            1462




            1462























                7














                Use exec bash at the end




                A bash script operates on its current environment or on that of its
                children, but never on its parent environment.




                However, this question often gets asked because one wants to be left at the bash prompt in a certain directory after the execution of a bash script from another directory.



                If this is the case, simply execute a child bash instance at the end of the script:



                #!/bin/bash
                cd desired/directory
                exec bash





                share|improve this answer


























                • You are a genius!

                  – Kasper
                  Aug 15 '16 at 3:05











                • Better to just source the script, as in accepted answer: using exec is typically considered the last resort of a scoundrel.. :)

                  – neuronet
                  Aug 16 '16 at 0:18











                • this trick doesn't work in debian 9 stretch.

                  – vdegenne
                  May 5 '18 at 0:41













                • This is the wrong way to go about this!

                  – Dennis Williamson
                  4 hours ago
















                7














                Use exec bash at the end




                A bash script operates on its current environment or on that of its
                children, but never on its parent environment.




                However, this question often gets asked because one wants to be left at the bash prompt in a certain directory after the execution of a bash script from another directory.



                If this is the case, simply execute a child bash instance at the end of the script:



                #!/bin/bash
                cd desired/directory
                exec bash





                share|improve this answer


























                • You are a genius!

                  – Kasper
                  Aug 15 '16 at 3:05











                • Better to just source the script, as in accepted answer: using exec is typically considered the last resort of a scoundrel.. :)

                  – neuronet
                  Aug 16 '16 at 0:18











                • this trick doesn't work in debian 9 stretch.

                  – vdegenne
                  May 5 '18 at 0:41













                • This is the wrong way to go about this!

                  – Dennis Williamson
                  4 hours ago














                7












                7








                7







                Use exec bash at the end




                A bash script operates on its current environment or on that of its
                children, but never on its parent environment.




                However, this question often gets asked because one wants to be left at the bash prompt in a certain directory after the execution of a bash script from another directory.



                If this is the case, simply execute a child bash instance at the end of the script:



                #!/bin/bash
                cd desired/directory
                exec bash





                share|improve this answer















                Use exec bash at the end




                A bash script operates on its current environment or on that of its
                children, but never on its parent environment.




                However, this question often gets asked because one wants to be left at the bash prompt in a certain directory after the execution of a bash script from another directory.



                If this is the case, simply execute a child bash instance at the end of the script:



                #!/bin/bash
                cd desired/directory
                exec bash






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 3 hours ago

























                answered Apr 21 '16 at 11:36









                Serge StroobandtSerge Stroobandt

                84321327




                84321327













                • You are a genius!

                  – Kasper
                  Aug 15 '16 at 3:05











                • Better to just source the script, as in accepted answer: using exec is typically considered the last resort of a scoundrel.. :)

                  – neuronet
                  Aug 16 '16 at 0:18











                • this trick doesn't work in debian 9 stretch.

                  – vdegenne
                  May 5 '18 at 0:41













                • This is the wrong way to go about this!

                  – Dennis Williamson
                  4 hours ago



















                • You are a genius!

                  – Kasper
                  Aug 15 '16 at 3:05











                • Better to just source the script, as in accepted answer: using exec is typically considered the last resort of a scoundrel.. :)

                  – neuronet
                  Aug 16 '16 at 0:18











                • this trick doesn't work in debian 9 stretch.

                  – vdegenne
                  May 5 '18 at 0:41













                • This is the wrong way to go about this!

                  – Dennis Williamson
                  4 hours ago

















                You are a genius!

                – Kasper
                Aug 15 '16 at 3:05





                You are a genius!

                – Kasper
                Aug 15 '16 at 3:05













                Better to just source the script, as in accepted answer: using exec is typically considered the last resort of a scoundrel.. :)

                – neuronet
                Aug 16 '16 at 0:18





                Better to just source the script, as in accepted answer: using exec is typically considered the last resort of a scoundrel.. :)

                – neuronet
                Aug 16 '16 at 0:18













                this trick doesn't work in debian 9 stretch.

                – vdegenne
                May 5 '18 at 0:41







                this trick doesn't work in debian 9 stretch.

                – vdegenne
                May 5 '18 at 0:41















                This is the wrong way to go about this!

                – Dennis Williamson
                4 hours ago





                This is the wrong way to go about this!

                – Dennis Williamson
                4 hours ago











                3














                If you change between directories far away in the filesystem. I will recommend autojump.






                share|improve this answer




























                  3














                  If you change between directories far away in the filesystem. I will recommend autojump.






                  share|improve this answer


























                    3












                    3








                    3







                    If you change between directories far away in the filesystem. I will recommend autojump.






                    share|improve this answer













                    If you change between directories far away in the filesystem. I will recommend autojump.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 23 '11 at 2:30









                    stnlystnly

                    34624




                    34624























                        2














                        Depends on what you're going to do, another solution can be creating a function instead of a script.



                        Example:



                        Create a function in a file, let's say /home/aidin/my-cd-script:



                        function my-cd() {
                        cd /to/my/path
                        }


                        Then include it in your bashrc or zshrc file:



                        # Somewhere in rc file
                        source /home/aidin/my-cd-script


                        Now you can use it like a command:



                        $ my-cd





                        share|improve this answer






























                          2














                          Depends on what you're going to do, another solution can be creating a function instead of a script.



                          Example:



                          Create a function in a file, let's say /home/aidin/my-cd-script:



                          function my-cd() {
                          cd /to/my/path
                          }


                          Then include it in your bashrc or zshrc file:



                          # Somewhere in rc file
                          source /home/aidin/my-cd-script


                          Now you can use it like a command:



                          $ my-cd





                          share|improve this answer




























                            2












                            2








                            2







                            Depends on what you're going to do, another solution can be creating a function instead of a script.



                            Example:



                            Create a function in a file, let's say /home/aidin/my-cd-script:



                            function my-cd() {
                            cd /to/my/path
                            }


                            Then include it in your bashrc or zshrc file:



                            # Somewhere in rc file
                            source /home/aidin/my-cd-script


                            Now you can use it like a command:



                            $ my-cd





                            share|improve this answer















                            Depends on what you're going to do, another solution can be creating a function instead of a script.



                            Example:



                            Create a function in a file, let's say /home/aidin/my-cd-script:



                            function my-cd() {
                            cd /to/my/path
                            }


                            Then include it in your bashrc or zshrc file:



                            # Somewhere in rc file
                            source /home/aidin/my-cd-script


                            Now you can use it like a command:



                            $ my-cd






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jun 20 '18 at 1:36









                            muru

                            1




                            1










                            answered Jun 20 '18 at 0:59









                            AidinAidin

                            1943




                            1943






























                                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%2f27139%2fscript-to-change-current-directory-cd-pwd%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

                                Loup dans la culture

                                How to solve the problem of ntp “Unable to contact time server” from KDE?

                                ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux