If I understand you correctly, you want to add any directories
$X is the
$PWD or any of its ancestors.
The script at the end of this post should give the behaviour you want. You need to source it in every session where you want it. If you name the file
augment_path.sh, then adding this line to your
.bashrc should be sufficient:
I think garyjohn has the basic approach right, but he's searching all descendents rather than all ancestors.
$PROMPT_COMMAND variable allows you to specify a command to be executed each time the prompt is displayed. I've added a
$PROMPT_COMMAND_OLD variable to allow the original
$PROMPT_COMMAND to be restored
It's probably not necessary, but for good form I add a
$LAST_WD variable and test to avoid recomputing the path when the directory hasn't changed. You can remove all three lines containing
LAST_WD if you like.
augment_path function scans from
$PWD upwards, looking for the target directories in each ancestor and adding any it finds to the path.
They are placed in the path in order, so the deepest such directory will take precedence if there are any conflicts. I assume this is the desired behaviour. If not, change
However, these directories will all take precedence over the rest of the path. If you want the rest of the path to take precedence, change:
if [ "$PWD" = "$LAST_WD" ]; then return 0; fi;
until [ "$scandir" = "" ]; do
if [ -d "$resolved_target" ]; then