Changing current directory with a bash script? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite













This question already has an answer here:



  • Script to change current directory (cd, pwd)

    6 answers



I was wondering if it's possible to change directory of a shell from a bash script, but keep the directory change persistent to more than just the subshell.



I'm aware that when you run cd inside a bash script the directory will only change inside the subshell, and when you get back out you'll return to whatever directory you were in.



However, I want to make a command to bring me to certain directories. I could use an alias, but there are a lot of subdirectories that I would have to make an alias for..










share|improve this question













marked as duplicate by Jeff Schaller, JigglyNaga, schily, Jesse_b, RalfFriedl yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Only if you source the script, rather than execute it.
    – jordanm
    yesterday










  • Unless your source such a script, you can't make the changes reflect in the parent shell
    – Inian
    yesterday






  • 1




    So make a function.
    – muru
    yesterday










  • This surely must be a dup?
    – roaima
    yesterday














up vote
2
down vote

favorite













This question already has an answer here:



  • Script to change current directory (cd, pwd)

    6 answers



I was wondering if it's possible to change directory of a shell from a bash script, but keep the directory change persistent to more than just the subshell.



I'm aware that when you run cd inside a bash script the directory will only change inside the subshell, and when you get back out you'll return to whatever directory you were in.



However, I want to make a command to bring me to certain directories. I could use an alias, but there are a lot of subdirectories that I would have to make an alias for..










share|improve this question













marked as duplicate by Jeff Schaller, JigglyNaga, schily, Jesse_b, RalfFriedl yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Only if you source the script, rather than execute it.
    – jordanm
    yesterday










  • Unless your source such a script, you can't make the changes reflect in the parent shell
    – Inian
    yesterday






  • 1




    So make a function.
    – muru
    yesterday










  • This surely must be a dup?
    – roaima
    yesterday












up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:



  • Script to change current directory (cd, pwd)

    6 answers



I was wondering if it's possible to change directory of a shell from a bash script, but keep the directory change persistent to more than just the subshell.



I'm aware that when you run cd inside a bash script the directory will only change inside the subshell, and when you get back out you'll return to whatever directory you were in.



However, I want to make a command to bring me to certain directories. I could use an alias, but there are a lot of subdirectories that I would have to make an alias for..










share|improve this question














This question already has an answer here:



  • Script to change current directory (cd, pwd)

    6 answers



I was wondering if it's possible to change directory of a shell from a bash script, but keep the directory change persistent to more than just the subshell.



I'm aware that when you run cd inside a bash script the directory will only change inside the subshell, and when you get back out you'll return to whatever directory you were in.



However, I want to make a command to bring me to certain directories. I could use an alias, but there are a lot of subdirectories that I would have to make an alias for..





This question already has an answer here:



  • Script to change current directory (cd, pwd)

    6 answers







bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









kamziro

1213




1213




marked as duplicate by Jeff Schaller, JigglyNaga, schily, Jesse_b, RalfFriedl yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, JigglyNaga, schily, Jesse_b, RalfFriedl yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    Only if you source the script, rather than execute it.
    – jordanm
    yesterday










  • Unless your source such a script, you can't make the changes reflect in the parent shell
    – Inian
    yesterday






  • 1




    So make a function.
    – muru
    yesterday










  • This surely must be a dup?
    – roaima
    yesterday












  • 1




    Only if you source the script, rather than execute it.
    – jordanm
    yesterday










  • Unless your source such a script, you can't make the changes reflect in the parent shell
    – Inian
    yesterday






  • 1




    So make a function.
    – muru
    yesterday










  • This surely must be a dup?
    – roaima
    yesterday







1




1




Only if you source the script, rather than execute it.
– jordanm
yesterday




Only if you source the script, rather than execute it.
– jordanm
yesterday












Unless your source such a script, you can't make the changes reflect in the parent shell
– Inian
yesterday




Unless your source such a script, you can't make the changes reflect in the parent shell
– Inian
yesterday




1




1




So make a function.
– muru
yesterday




So make a function.
– muru
yesterday












This surely must be a dup?
– roaima
yesterday




This surely must be a dup?
– roaima
yesterday










2 Answers
2






active

oldest

votes

















up vote
1
down vote













No, scripts are executed in a separate shell, which doesn't affect its ancestors. But you could use a function, which takes arguments, and so is more flexible than alias. Another option is sourcing files with source or its equivalent ., but that doesn't accept arguments. Still, it affects the current shell. You might combine the two and put functions in a file that you'd source and then use the functions in the current process.






share|improve this answer



























    up vote
    0
    down vote













    You probably are looking for something like the CDPATH shell variable.



    The CDPATH variable acts like PATH but for the cd command.



    Setting with something like



    CDPATH=".:~:~/projects:~/music"


    would allow you to say



    cd ricky_martin


    anywhere, and it would go through the :-separated directory paths in the $CDPATH value in order until it found a subdirectory called ricky_martin somewhere (possibly ~/music/ricky_martin) and then cd there.



    Likewise



    cd world_domination


    may take you to ~/projects/world_domination if there is such a subdirectory. If world_domination also existed in the current directory, this directory would be selected first as it occurs earlier in $CDPATH (the dot in the first position).



    It would also be allowed to do



    cd proj1/tests


    from anywhere to get to ~/projects/proj1/tests if such a directory existed (with the above $CDPATH value, unless proj/tests did not exist in the current directory or in your home directory).



    Note that the CDPATH shell variable should not be exported as that may seriously confuse some scripts.



    The CDPATH variable is documented in the bash manual (man bash):




    CDPATH



    The search path for the cd command. This is a colon-separated
    list of directories in which the shell looks for destination
    directories specified by the cd command. A sample value is
    ".:~:/usr".







    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      No, scripts are executed in a separate shell, which doesn't affect its ancestors. But you could use a function, which takes arguments, and so is more flexible than alias. Another option is sourcing files with source or its equivalent ., but that doesn't accept arguments. Still, it affects the current shell. You might combine the two and put functions in a file that you'd source and then use the functions in the current process.






      share|improve this answer
























        up vote
        1
        down vote













        No, scripts are executed in a separate shell, which doesn't affect its ancestors. But you could use a function, which takes arguments, and so is more flexible than alias. Another option is sourcing files with source or its equivalent ., but that doesn't accept arguments. Still, it affects the current shell. You might combine the two and put functions in a file that you'd source and then use the functions in the current process.






        share|improve this answer






















          up vote
          1
          down vote










          up vote
          1
          down vote









          No, scripts are executed in a separate shell, which doesn't affect its ancestors. But you could use a function, which takes arguments, and so is more flexible than alias. Another option is sourcing files with source or its equivalent ., but that doesn't accept arguments. Still, it affects the current shell. You might combine the two and put functions in a file that you'd source and then use the functions in the current process.






          share|improve this answer












          No, scripts are executed in a separate shell, which doesn't affect its ancestors. But you could use a function, which takes arguments, and so is more flexible than alias. Another option is sourcing files with source or its equivalent ., but that doesn't accept arguments. Still, it affects the current shell. You might combine the two and put functions in a file that you'd source and then use the functions in the current process.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Tomasz

          8,72552862




          8,72552862






















              up vote
              0
              down vote













              You probably are looking for something like the CDPATH shell variable.



              The CDPATH variable acts like PATH but for the cd command.



              Setting with something like



              CDPATH=".:~:~/projects:~/music"


              would allow you to say



              cd ricky_martin


              anywhere, and it would go through the :-separated directory paths in the $CDPATH value in order until it found a subdirectory called ricky_martin somewhere (possibly ~/music/ricky_martin) and then cd there.



              Likewise



              cd world_domination


              may take you to ~/projects/world_domination if there is such a subdirectory. If world_domination also existed in the current directory, this directory would be selected first as it occurs earlier in $CDPATH (the dot in the first position).



              It would also be allowed to do



              cd proj1/tests


              from anywhere to get to ~/projects/proj1/tests if such a directory existed (with the above $CDPATH value, unless proj/tests did not exist in the current directory or in your home directory).



              Note that the CDPATH shell variable should not be exported as that may seriously confuse some scripts.



              The CDPATH variable is documented in the bash manual (man bash):




              CDPATH



              The search path for the cd command. This is a colon-separated
              list of directories in which the shell looks for destination
              directories specified by the cd command. A sample value is
              ".:~:/usr".







              share|improve this answer


























                up vote
                0
                down vote













                You probably are looking for something like the CDPATH shell variable.



                The CDPATH variable acts like PATH but for the cd command.



                Setting with something like



                CDPATH=".:~:~/projects:~/music"


                would allow you to say



                cd ricky_martin


                anywhere, and it would go through the :-separated directory paths in the $CDPATH value in order until it found a subdirectory called ricky_martin somewhere (possibly ~/music/ricky_martin) and then cd there.



                Likewise



                cd world_domination


                may take you to ~/projects/world_domination if there is such a subdirectory. If world_domination also existed in the current directory, this directory would be selected first as it occurs earlier in $CDPATH (the dot in the first position).



                It would also be allowed to do



                cd proj1/tests


                from anywhere to get to ~/projects/proj1/tests if such a directory existed (with the above $CDPATH value, unless proj/tests did not exist in the current directory or in your home directory).



                Note that the CDPATH shell variable should not be exported as that may seriously confuse some scripts.



                The CDPATH variable is documented in the bash manual (man bash):




                CDPATH



                The search path for the cd command. This is a colon-separated
                list of directories in which the shell looks for destination
                directories specified by the cd command. A sample value is
                ".:~:/usr".







                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You probably are looking for something like the CDPATH shell variable.



                  The CDPATH variable acts like PATH but for the cd command.



                  Setting with something like



                  CDPATH=".:~:~/projects:~/music"


                  would allow you to say



                  cd ricky_martin


                  anywhere, and it would go through the :-separated directory paths in the $CDPATH value in order until it found a subdirectory called ricky_martin somewhere (possibly ~/music/ricky_martin) and then cd there.



                  Likewise



                  cd world_domination


                  may take you to ~/projects/world_domination if there is such a subdirectory. If world_domination also existed in the current directory, this directory would be selected first as it occurs earlier in $CDPATH (the dot in the first position).



                  It would also be allowed to do



                  cd proj1/tests


                  from anywhere to get to ~/projects/proj1/tests if such a directory existed (with the above $CDPATH value, unless proj/tests did not exist in the current directory or in your home directory).



                  Note that the CDPATH shell variable should not be exported as that may seriously confuse some scripts.



                  The CDPATH variable is documented in the bash manual (man bash):




                  CDPATH



                  The search path for the cd command. This is a colon-separated
                  list of directories in which the shell looks for destination
                  directories specified by the cd command. A sample value is
                  ".:~:/usr".







                  share|improve this answer














                  You probably are looking for something like the CDPATH shell variable.



                  The CDPATH variable acts like PATH but for the cd command.



                  Setting with something like



                  CDPATH=".:~:~/projects:~/music"


                  would allow you to say



                  cd ricky_martin


                  anywhere, and it would go through the :-separated directory paths in the $CDPATH value in order until it found a subdirectory called ricky_martin somewhere (possibly ~/music/ricky_martin) and then cd there.



                  Likewise



                  cd world_domination


                  may take you to ~/projects/world_domination if there is such a subdirectory. If world_domination also existed in the current directory, this directory would be selected first as it occurs earlier in $CDPATH (the dot in the first position).



                  It would also be allowed to do



                  cd proj1/tests


                  from anywhere to get to ~/projects/proj1/tests if such a directory existed (with the above $CDPATH value, unless proj/tests did not exist in the current directory or in your home directory).



                  Note that the CDPATH shell variable should not be exported as that may seriously confuse some scripts.



                  The CDPATH variable is documented in the bash manual (man bash):




                  CDPATH



                  The search path for the cd command. This is a colon-separated
                  list of directories in which the shell looks for destination
                  directories specified by the cd command. A sample value is
                  ".:~:/usr".








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  Kusalananda

                  115k15218351




                  115k15218351












                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?