I would like to change my $PATH
environment variable dependent on my current working directory.
Say I'm in /foo/bar/baz
and I have the directories /foo/node_modules/.bin
and /foo/bar/baz/node_modules/.bin
. I would like to add every possible ./node_modules/.bin
recursively to $PATH
.
But when I cd
into a different directory (like /foo/bar
), I want my original, clean $PATH
to be restored, and then to start to look for ./node_modules/.bin
recursively again.
(I want to solve my own question from npm's issue tracker: Can we add locally installed packages to PATH, too?)
Note: I'm on a Mac, but interested in a general solution.
Best Answer
Introduction
If I understand you correctly, you want to add any directories
"$X/node_modules/.bin"
where$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:Discussion
I think garyjohn has the basic approach right, but he's searching all descendents rather than all ancestors.
The
$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 restoredIt'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 containingLAST_WD
if you like.The
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
to
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:
to
Script