在你的bashrc中设置:
PROMPT_COMMAND='[[ $PWD == "/foo/bar/" ]] && export FOO=BAR || unset FOO'
PROMPT_COMMAND变量的内容将在每次重写提示时执行(在实际写入之前)上面的命令检查$PWD变量(它保存shell的当前工作目录)对“/ foo / bar”如果它匹配它导出你的变量,如果没有,那么变量是未设置的.
例如
peteches@yog-sothoth$PROMPT_COMMAND='[[ $PWD == "/home/peteches/test" ]] && export FOO=BAR || unset FOO'
peteches@yog-sothoth$pwd
/home/peteches
peteches@yog-sothoth$cd test
peteches@yog-sothoth$pwd
/home/peteches/test
peteches@yog-sothoth$env | grep FOO
6:FOO=BAR
73:PROMPT_COMMAND=[[ $PWD == "/home/peteches/test" ]] && export FOO=BAR || unset FOO
peteches@yog-sothoth$cd ../
peteches@yog-sothoth$pwd
/home/peteches
peteches@yog-sothoth$env | grep FOO
72:PROMPT_COMMAND=[[ $PWD == "/home/peteches/test" ]] && export FOO=BAR || unset FOO
peteches@yog-sothoth$