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

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

    To make a bash script that will cd to a select directory :

    Create the script file

    #!/bin/sh
    # file : /scripts/cdjava
    #
    cd /home/askgelal/projects/java
    

    Then create an alias in your startup file.

    #!/bin/sh
    # file /scripts/mastercode.sh
    #
    alias cdjava='. /scripts/cdjava'
    

    • I created a startup file where I dump all my aliases and custom functions.
    • Then I source this file into my .bashrc to have it set on each boot.

    For example, create a master aliases/functions file: /scripts/mastercode.sh
    (Put the alias in this file.)

    Then at the end of your .bashrc file:

    source /scripts/mastercode.sh
    



    Now its easy to cd to your java directory, just type cdjava and you are there.

提交回复
热议问题