Why I can't change directories using “cd”?

后端 未结 30 2371
眼角桃花
眼角桃花 2020-11-21 06:17

I\'m trying to write a small script to change the current directory to my project directory:

#!/bin/bash
cd /home/tree/projects/java

I save

相关标签:
30条回答
  • 2020-11-21 06:31

    You can do following:

    #!/bin/bash
    cd /your/project/directory
    # start another shell and replacing the current
    exec /bin/bash
    

    EDIT: This could be 'dotted' as well, to prevent creation of subsequent shells.

    Example:

    . ./previous_script  (with or without the first line)
    
    0 讨论(0)
  • 2020-11-21 06:32

    You can combine Adam & Greg's alias and dot approaches to make something that can be more dynamic—

    alias project=". project"
    

    Now running the project alias will execute the project script in the current shell as opposed to the subshell.

    0 讨论(0)
  • 2020-11-21 06:32

    As explained on the other answers, you have changed the directory, but only within the sub-shell that runs the script. this does not impact the parent shell.

    One solution is to use bash functions instead of a bash script (sh); by placing your bash script code into a function. That makes the function available as a command and then, this will be executed without a child process and thus any cd command will impact the caller shell.

    Bash functions :

    One feature of the bash profile is to store custom functions that can be run in the terminal or in bash scripts the same way you run application/commands this also could be used as a shortcut for long commands.

    To make your function efficient system widely you will need to copy your function at the end of several files

    /home/user/.bashrc
    /home/user/.bash_profile
    /root/.bashrc
    /root/.bash_profile
    

    You can sudo kwrite /home/user/.bashrc /home/user/.bash_profile /root/.bashrc /root/.bash_profile to edit/create those files quickly

    Howto :

    Copy your bash script code inside a new function at the end of your bash's profile file and restart your terminal, you can then run cdd or whatever the function you wrote.

    Script Example

    Making shortcut to cd .. with cdd

    cdd() {
      cd ..
    }
    

    ls shortcut

    ll() {
      ls -l -h
    }
    

    ls shortcut

    lll() {
      ls -l -h -a
    }
    
    0 讨论(0)
  • 2020-11-21 06:33

    It only changes the directory for the script itself, while your current directory stays the same.

    You might want to use a symbolic link instead. It allows you to make a "shortcut" to a file or directory, so you'd only have to type something like cd my-project.

    0 讨论(0)
  • 2020-11-21 06:36

    While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.

    Another way to do, is to implement your script as a function in bash.

    function cdbm() {
      cd whereever_you_want_to_go
      echo "Arguments to the functions were $1, $2, ..."
    }
    

    This technique is used by autojump: http://github.com/joelthelion/autojump/wiki to provide you with learning shell directory bookmarks.

    0 讨论(0)
  • 2020-11-21 06:40

    On my particular case i needed too many times to change for the same directory. So on my .bashrc (I use ubuntu) i've added the

    1 -

    $ nano ~./bashrc

     function switchp
     {
        cd /home/tree/projects/$1
     }
    

    2-

    $ source ~/.bashrc

    3 -

    $ switchp java

    Directly it will do: cd /home/tree/projects/java

    Hope that helps!

    0 讨论(0)
提交回复
热议问题