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

后端 未结 30 2900
眼角桃花
眼角桃花 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 06:24

    Jeremy Ruten's idea of using a symlink triggered a thought that hasn't crossed any other answer. Use:

    CDPATH=:$HOME/projects
    

    The leading colon is important; it means that if there is a directory 'dir' in the current directory, then 'cd dir' will change to that, rather than hopping off somewhere else. With the value set as shown, you can do:

    cd java
    

    and, if there is no sub-directory called java in the current directory, then it will take you directly to $HOME/projects/java - no aliases, no scripts, no dubious execs or dot commands.

    My $HOME is /Users/jleffler; my $CDPATH is:

    :/Users/jleffler:/Users/jleffler/mail:/Users/jleffler/src:/Users/jleffler/src/perl:/Users/jleffler/src/sqltools:/Users/jleffler/lib:/Users/jleffler/doc:/Users/jleffler/work
    

提交回复
热议问题