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/repository/foo/" ]]; then set_foo_env.sh fi } I'm not 100% sure if the environment variables will be correctly passed down to the shell.
Edit: it works