Ignoring the CDPATH
environment variable for the time being, the name after cd
is a directory path name. There are only two sorts of path name in Unix:
- absolute names that begin with a
/
- relative names which don't start with a
/
All relative names are treated by the kernel as relative to the current directory — as if the name was prefixed by ./
.
If you have CDPATH
set, it complicates the search, and it is then conceivable that cd somewhere
and cd ./somewhere
land you in different directories. A directory name with no leading /
and with no leading .
or ..
component (where cd .hidden
doesn't count — the leading component is .hidden
— but cd ./visible
or cd ../visible
do count) is searched for using CDPATH
.
For example, consider this tree structure (only directories shown):
. - current directory
./somewhere
./src
./src/somewhere
Suppose you have CDPATH=src:
. Then cd somewhere
will go to ./src/somewhere
and cd ./somewhere
will go (unsurprisingly) to ./somewhere
. If you type a name such as cd src/somewhere
, the cd
command will search for a sub-directory /xyz/pqr/src/somewhere
for each directory /xyz/pqr
on CDPATH
in turn. I perpetually use CDPATH
(7 directories on my own machines; 14 on my work machines).
- Rule of thumb: use
CDPATH=:…whatever…
That puts the current directory first on the search path, and is usually the most sane behaviour.
A similar story applies to searching for ordinary external (non-aliased, non-function, non-builtin) commands. Command names that start with a slash are absolute pathnames; command names containing a slash are relative to the current directory; command names without a slash are searched for on $PATH
.