CDPATH in Bash
What is CDPATH?
CDPATH is a bash environment variable that defines search paths for
the cd
command. When you run cd dirname
, bash
searches through each directory in CDPATH to find a matching
subdirectory.
You can set it in your .bashrc file.
The Critical Details: Leading Colon and No Export
To prioritize the current working directory, you
must put :
at the beginning of CDPATH:
CDPATH=":${HOME}:${HOME}/Projects/"
Without the leading colon, bash will search CDPATH directories first, potentially jumping to unexpected locations (don’t ask how I know).
Also, avoid exportng it. It would certainly break scripts with unwanted consequences…
Practical Usage
# From anywhere, these will work:
cd myproject # Finds ~/Projects/myproject
cd Documents # Finds ~/Documents
cd local-dir # Finds ./local-dir (thanks to leading colon)
Created: 2025-08-07
--
Updated: 2025-08-07