You could use PROMPT_COMMAND, the code specified in PROMPT_COMMAND is executed before bash draws the prompt.
# Call function 'foo' PROMPT_COMMAND=foo # The function foo() { if [[ $PWD == "/home/$HOME"$HOME/repository/foo/"foo" ]]; then set_foo_env.sh fi } I'mNote, PROMPT_COMMAND is bash specific, and this is not 100% sure ifa efficient way because the environment variablesfunction foo will be correctly passed down tocalled every time bash draws the shellprompt.
Edit: it works