I want to use bash's CDPATH
to point to a directory of symlinks to directories that I access frequently. However, doing:
export CDPATH="~/symlinks"
causes cd SUBDIR
to stop working if ./SUBDIR
and ~/symlinks/SUBDIR
both exist; CDPATH
directories take precedence over the current working directory.
I tried to fix this by instead using:
export CDPATH=".:~/symlinks"
and that does fix the precedence problem, but now cd
ing to a subdirectory always prints its full path:
$ pwd
/foo/bar
$ cd baz
/foo/bar/baz
This is a bit annoying. I know that I can suppress all cd
output by doing alias cd='> /dev/null cd'
, but I do like the path being printed for other CDPATH
entries (or when doing cd -
). Is there anything better that I can do?
Best Answer
Two clues:
If
CDPATH
is non-existent or an empty string, thencd SUBDIR
works fine and does not print extra spew.The bash manpage says:
The manpage seems to be oversimplifying: clearly a null directory name (i.e., an empty string) is not exactly the same as
.
sinceCDPATH=.
generates extra output butCDPATH=
does not. However, since null directories are legal inCDPATH
, and since a null directory doesn't generate extra output, we therefore can use:Testing (with bash 4.4.12) confirms that behaves as desired:
cd SUBDIR
changes to./SUBDIR
instead of to~/symlinks/SUBDIR
and does not print any extra spew.