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

后端 未结 30 2290
眼角桃花
眼角桃花 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:45

    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 a (new) bash prompt in a certain directory after 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:

    #!/usr/bin/env bash
    cd /home/tree/projects/java
    exec bash
    

    Update

    At least with newer versions of bash, the exec on the last line is no longer required. Furthermore, the script could be made to work with whatever preferred shell by using the $SHELL environment variable. This then gives:

    #!/usr/bin/env bash
    cd desired/directory
    $SHELL
    
    0 讨论(0)
  • 2020-11-21 06:45

    I did the following:

    create a file called case

    paste the following in the file:

    #!/bin/sh
    
    cd /home/"$1"
    

    save it and then:

    chmod +x case
    

    I also created an alias in my .bashrc:

    alias disk='cd /home/; . case'
    

    now when I type:

    case 12345
    

    essentially I am typing:

    cd /home/12345
    

    You can type any folder after 'case':

    case 12
    
    case 15
    
    case 17
    

    which is like typing:

    cd /home/12
    
    cd /home/15
    
    cd /home/17
    

    respectively

    In my case the path is much longer - these guys summed it up with the ~ info earlier.

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

    I have to work in tcsh, and I know this is not an elegant solution, but for example, if I had to change folders to a path where one word is different, the whole thing can be done in the alias

    a alias_name 'set a = `pwd`; set b = `echo $a | replace "Trees" "Tests"` ; cd $b'
    

    If the path is always fixed, the just

    a alias_name2 'cd path/you/always/need'
    

    should work In the line above, the new folder path is set

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

    I have a simple bash script called p to manage directory changing on
    github.com/godzilla/bash-stuff
    just put the script in your local bin directory (/usr/local/bin)
    and put

    alias p='. p'
    

    in your .bashrc

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

    simply run:

    cd /home/xxx/yyy && command_you_want
    
    0 讨论(0)
  • 2020-11-21 06:51

    If you are using fish as your shell, the best solution is to create a function. As an example, given the original question, you could copy the 4 lines below and paste them into your fish command line:

    function proj
       cd /home/tree/projects/java
    end
    funcsave proj
    

    This will create the function and save it for use later. If your project changes, just repeat the process using the new path.

    If you prefer, you can manually add the function file by doing the following:

    nano ~/.config/fish/functions/proj.fish
    

    and enter the text:

    function proj
       cd /home/tree/projects/java
    end
    

    and finally press ctrl+x to exit and y followed by return to save your changes.

    (NOTE: the first method of using funcsave creates the proj.fish file for you).

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