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?

  • No direct answer to your question, but for the case of your scripts you could define an alias that runs scripts/cleancode.py from the root directory of the repository you're in, something like alias cleancode='"$(git rev-parse --show-toplevel)"/scripts/cleancode.py'.
    – DonHolgo
    Feb 19, 2021 at 9:34

1 Answer 1


You can create a shell script cleancode in your PATH and do something like that :



while [ "x$cwd" != x/ ]; do
    if [ -e $cwd/scripts/cleancode.py ]; then
        # do stuff here
    cwd=`dirname $cwd`

Note the / directory is not checked since the loop is broken before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.