In the manual, CDPATH
is described this way:
The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is ".:~:/usr".
For completeness, here is some experiment similar to
terdon's.
$~> mkdir /tmp/2 ./2 ./3
$~> cd 2
$~/2> cd ..
$~> CDPATH=/tmp
$~> cd 2
/tmp/2
$~> cd ~
$~> cd 3
$~/3>
As you can see, after setting CDPATH=/tmp
, Bash
looks in /tmp
first for possible target directories.
If not found in /tmp
, it tries looking in the current directory.
We could also note that (Shell Builtins)
If a non-empty directory name from CDPATH is used, or if - is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output.
I also want to share this:
$~> CDPATH=.:/tmp
$~> cd 2
/home/myuser/2
$~/2> cd 2
/tmp/2
In this continuation, CDPATH
has been given two directories.
The first is .
, i.e., the current directory.
Since it comes first, upon trying cd 2
, we go
to the /home/myuser/2
, although /tmp/2
also exists.
It is like $PATH
, the first listed directories take precedence.