script to remember dir and always cd to it instead of the root dirchanging current working dir with a scriptShell Script for going through a dir recursively and chmodding based on conditions of file typePerl script, do cd on terminalHow to change the working directory of invoking shell using a script?Is it possible to change the PS1 periodically by a script in the background?bash script not creating alias and not updating $PS1Symbolic link to change directory only works from home dirBash script to replace two steps: cd ./some_dir, ls -al?Script to validate home dirHow do I make a cd command take effect outside of the bash script subshell?

Minkowski space

What are the differences between the usage of 'it' and 'they'?

Fencing style for blades that can attack from a distance

A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?

Writing rule stating superpower from different root cause is bad writing

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

Can I ask the recruiters in my resume to put the reason why I am rejected?

Why dont electromagnetic waves interact with each other?

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

The use of multiple foreign keys on same column in SQL Server

How much RAM could one put in a typical 80386 setup?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Why does Kotter return in Welcome Back Kotter?

Why can't I see bouncing of a switch on an oscilloscope?

What does "Puller Prush Person" mean?

How does strength of boric acid solution increase in presence of salicylic acid?

How does one intimidate enemies without having the capacity for violence?

How to find program name(s) of an installed package?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

Adding span tags within wp_list_pages list items

LaTeX closing $ signs makes cursor jump

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

Test if tikzmark exists on same page

Is it legal for company to use my work email to pretend I still work there?



script to remember dir and always cd to it instead of the root dir


changing current working dir with a scriptShell Script for going through a dir recursively and chmodding based on conditions of file typePerl script, do cd on terminalHow to change the working directory of invoking shell using a script?Is it possible to change the PS1 periodically by a script in the background?bash script not creating alias and not updating $PS1Symbolic link to change directory only works from home dirBash script to replace two steps: cd ./some_dir, ls -al?Script to validate home dirHow do I make a cd command take effect outside of the bash script subshell?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















How can write a script to change to a given directory but also remember it so that when you do cd it always changes to that directory ?



#!/bin/bash
setdir()
cd $1
# remember the directory we are changing to here so whenever we do cd we go back to this set dir


setdir "$1"









share|improve this question






























    3















    How can write a script to change to a given directory but also remember it so that when you do cd it always changes to that directory ?



    #!/bin/bash
    setdir()
    cd $1
    # remember the directory we are changing to here so whenever we do cd we go back to this set dir


    setdir "$1"









    share|improve this question


























      3












      3








      3








      How can write a script to change to a given directory but also remember it so that when you do cd it always changes to that directory ?



      #!/bin/bash
      setdir()
      cd $1
      # remember the directory we are changing to here so whenever we do cd we go back to this set dir


      setdir "$1"









      share|improve this question
















      How can write a script to change to a given directory but also remember it so that when you do cd it always changes to that directory ?



      #!/bin/bash
      setdir()
      cd $1
      # remember the directory we are changing to here so whenever we do cd we go back to this set dir


      setdir "$1"






      bash shell-script cd-command






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '13 at 23:53









      Gilles

      546k12911111624




      546k12911111624










      asked Nov 25 '13 at 21:29









      nixgadgetsnixgadgets

      1184




      1184




















          2 Answers
          2






          active

          oldest

          votes


















          5














          Something like the following should work:



          setdir() 
          cd "$1"
          export SETDIR_DEFAULT="$1"


          my_cd()
          cd "$1-$SETDIR_DEFAULT-$HOME"



          Note that these are functions, not a separate script. You can't do that from a separate script, since it would not be able to affect the parent shell that calls it.



          If you really want to override cd (please, don't do that), replace cd with builtin cd.






          share|improve this answer
































            1














            I know it might be a bit late for the answer, but you might like the idea of CDPATH vaiable. It allows cd to refer to content of directories in this variable from anywhere. Here's an example:



            $ mkdir -p test/1,2,3
            $ cd test/
            $ mkdir 1/a,b,c
            $ export CDPATH=/tmp/test/1
            $ ls
            1 2 3
            $ cd a
            $ pwd
            /tmp/test/1/a
            $ cd ~
            $ cd b
            $ pwd
            /tmp/test/1/b


            More details from man:



             CDPATH A <colon>-separated list of pathnames 
            that refer to directories. The cd utility
            shall use this list in its attempt to change
            the directory, as described in the DESCRIPTION.
            An empty string in place of a directory
            pathname represents the current directory. If
            CDPATH is not set, it shall be treated as if
            it were an empty string.





            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%2f102567%2fscript-to-remember-dir-and-always-cd-to-it-instead-of-the-root-dir%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              Something like the following should work:



              setdir() 
              cd "$1"
              export SETDIR_DEFAULT="$1"


              my_cd()
              cd "$1-$SETDIR_DEFAULT-$HOME"



              Note that these are functions, not a separate script. You can't do that from a separate script, since it would not be able to affect the parent shell that calls it.



              If you really want to override cd (please, don't do that), replace cd with builtin cd.






              share|improve this answer





























                5














                Something like the following should work:



                setdir() 
                cd "$1"
                export SETDIR_DEFAULT="$1"


                my_cd()
                cd "$1-$SETDIR_DEFAULT-$HOME"



                Note that these are functions, not a separate script. You can't do that from a separate script, since it would not be able to affect the parent shell that calls it.



                If you really want to override cd (please, don't do that), replace cd with builtin cd.






                share|improve this answer



























                  5












                  5








                  5







                  Something like the following should work:



                  setdir() 
                  cd "$1"
                  export SETDIR_DEFAULT="$1"


                  my_cd()
                  cd "$1-$SETDIR_DEFAULT-$HOME"



                  Note that these are functions, not a separate script. You can't do that from a separate script, since it would not be able to affect the parent shell that calls it.



                  If you really want to override cd (please, don't do that), replace cd with builtin cd.






                  share|improve this answer















                  Something like the following should work:



                  setdir() 
                  cd "$1"
                  export SETDIR_DEFAULT="$1"


                  my_cd()
                  cd "$1-$SETDIR_DEFAULT-$HOME"



                  Note that these are functions, not a separate script. You can't do that from a separate script, since it would not be able to affect the parent shell that calls it.



                  If you really want to override cd (please, don't do that), replace cd with builtin cd.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '13 at 23:54









                  Gilles

                  546k12911111624




                  546k12911111624










                  answered Nov 25 '13 at 21:55









                  Chris DownChris Down

                  81.6k15190204




                  81.6k15190204























                      1














                      I know it might be a bit late for the answer, but you might like the idea of CDPATH vaiable. It allows cd to refer to content of directories in this variable from anywhere. Here's an example:



                      $ mkdir -p test/1,2,3
                      $ cd test/
                      $ mkdir 1/a,b,c
                      $ export CDPATH=/tmp/test/1
                      $ ls
                      1 2 3
                      $ cd a
                      $ pwd
                      /tmp/test/1/a
                      $ cd ~
                      $ cd b
                      $ pwd
                      /tmp/test/1/b


                      More details from man:



                       CDPATH A <colon>-separated list of pathnames 
                      that refer to directories. The cd utility
                      shall use this list in its attempt to change
                      the directory, as described in the DESCRIPTION.
                      An empty string in place of a directory
                      pathname represents the current directory. If
                      CDPATH is not set, it shall be treated as if
                      it were an empty string.





                      share|improve this answer



























                        1














                        I know it might be a bit late for the answer, but you might like the idea of CDPATH vaiable. It allows cd to refer to content of directories in this variable from anywhere. Here's an example:



                        $ mkdir -p test/1,2,3
                        $ cd test/
                        $ mkdir 1/a,b,c
                        $ export CDPATH=/tmp/test/1
                        $ ls
                        1 2 3
                        $ cd a
                        $ pwd
                        /tmp/test/1/a
                        $ cd ~
                        $ cd b
                        $ pwd
                        /tmp/test/1/b


                        More details from man:



                         CDPATH A <colon>-separated list of pathnames 
                        that refer to directories. The cd utility
                        shall use this list in its attempt to change
                        the directory, as described in the DESCRIPTION.
                        An empty string in place of a directory
                        pathname represents the current directory. If
                        CDPATH is not set, it shall be treated as if
                        it were an empty string.





                        share|improve this answer

























                          1












                          1








                          1







                          I know it might be a bit late for the answer, but you might like the idea of CDPATH vaiable. It allows cd to refer to content of directories in this variable from anywhere. Here's an example:



                          $ mkdir -p test/1,2,3
                          $ cd test/
                          $ mkdir 1/a,b,c
                          $ export CDPATH=/tmp/test/1
                          $ ls
                          1 2 3
                          $ cd a
                          $ pwd
                          /tmp/test/1/a
                          $ cd ~
                          $ cd b
                          $ pwd
                          /tmp/test/1/b


                          More details from man:



                           CDPATH A <colon>-separated list of pathnames 
                          that refer to directories. The cd utility
                          shall use this list in its attempt to change
                          the directory, as described in the DESCRIPTION.
                          An empty string in place of a directory
                          pathname represents the current directory. If
                          CDPATH is not set, it shall be treated as if
                          it were an empty string.





                          share|improve this answer













                          I know it might be a bit late for the answer, but you might like the idea of CDPATH vaiable. It allows cd to refer to content of directories in this variable from anywhere. Here's an example:



                          $ mkdir -p test/1,2,3
                          $ cd test/
                          $ mkdir 1/a,b,c
                          $ export CDPATH=/tmp/test/1
                          $ ls
                          1 2 3
                          $ cd a
                          $ pwd
                          /tmp/test/1/a
                          $ cd ~
                          $ cd b
                          $ pwd
                          /tmp/test/1/b


                          More details from man:



                           CDPATH A <colon>-separated list of pathnames 
                          that refer to directories. The cd utility
                          shall use this list in its attempt to change
                          the directory, as described in the DESCRIPTION.
                          An empty string in place of a directory
                          pathname represents the current directory. If
                          CDPATH is not set, it shall be treated as if
                          it were an empty string.






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 days ago









                          rushrush

                          19.5k46596




                          19.5k46596



























                              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%2f102567%2fscript-to-remember-dir-and-always-cd-to-it-instead-of-the-root-dir%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

                              getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                              Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                              Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.