Change the cd into a function to run the real cd and run your script afterward.
cd () { if builtin cd ${1:+"$@"} && [ -r ./set_foo_env.sh ]; then . ./set_foo_env.sh fi } This assumes that:
- The
set_foo_env.shwould be specific to a single directories, and - That
set_foo_env.shshould change the existing shell's environment.
If you want path-based with a global file, then look at the question that manatwork mentioned ( Execute bash scripts on entering a directoryExecute bash scripts on entering a directory).