I have multiple git repositories on my machine. Each has a subdirectory called scripts/ with a script called cleancode.py.
Using bash (really git-bash), when I run cleancode.py I want it to execute the instance of cleancode.py from the scripts/ directory of the repo that my current working directory is within. My current hack approach is to have this in my path:
export PATH="$PATH:./scripts:./../scripts:./../../scripts:./../../../scripts:"
This works tolerably well. I suppose it could fail if I'm in a subdirectory more than three levels deep, but that's not a problem in practice. The real problem is that the first time I run cleancode.py it seems to remember the relative path of that execution and in future only looks for cleancode.py at that relative path. So if I run from the repo directory first then it works, and then if I cd to some subdir and run it again I get:
bash: ./scripts/cleancode.py: No such file or directory
because it's using the path at which it found it relative to where I was the first time I ran it. So what can I do to get it to not remember where it last found the script? Or can I get it to remember the absolute path thereto, rather than the relative path? Finally, is there a more robust way to deal with the issue as a whole, one that doesn't depend on adding four relative paths to my path? Thanks in advance.
Edit: I just found Disable bash's cache of executables in the path and set +h fixes the flaw in my hack. My actual question still stands, though: what is the most robust way to find the version of a command within the nearest relative path?
scripts/cleancode.py
from the root directory of the repository you're in, something likealias cleancode='"$(git rev-parse --show-toplevel)"/scripts/cleancode.py'
. – DonHolgo Feb 19 '21 at 09:34