Possible Duplicate:
Execute bash scripts on entering a directory

I am running an environment which requires some very specific tool versions. The commands to set up the environment are in a script called set_foo_env.sh

Suppose I have a directory ~/repository/foo/. Is is possible to run set_foo_env.sh as soon as I enter the foo directory?

  • The question, I accept might be similar. But one of the answers listed below here by @nsg is very interesting and novel. I am yet to try this solution out and rate it. I think this might work better than using aliasing cd although this is my personal opinion. Thanks for your comment though.
    – boffin
    Jul 10 '12 at 15:57
  • 4
    Depends on your goal. The PROMPT_COMMAND based solution will run the command after each command executed inside the specified directory. So if you do cd ~/repository/foo/, du -sch and date, then set_foo_env.sh will be executed 3 times. The cd aliasing solution will execute it only once.
    – manatwork
    Jul 10 '12 at 16:22

You could use PROMPT_COMMAND, the code specified in PROMPT_COMMAND is executed before bash draws the prompt.

# Call function 'foo'

# The function
foo() {
        if [[ $PWD == "$HOME/repository/foo" ]]; then

Note, PROMPT_COMMAND is bash specific, and this is not a efficient way because the function foo will be called every time bash draws the prompt.

  • 2
    Shouldn't that be either "$HOME/repository/foo" or "/home/$USER/repository/foo"? And the trailing slash may be a problem, at least my bash sets $PWD without it.
    – manatwork
    Jul 10 '12 at 16:16
  • 1
    Also, this seems to be bash-specific. It doesn't work on my old ksh.
    – rahmu
    Jul 10 '12 at 16:20
  • This isn't a good solution because it runs the command every time the prompt is displayed, not after a cd.
    – phemmer
    Jul 10 '12 at 21:52
  • Yes, PROMT_COMMAND is bash-specific. The question contains the tags 'bash' so i assumed he was a bash user. The cd-function trick suggested by Arcege may be a better solution, note the possible security concern stated by jw013.
    – nsg
    Jul 12 '12 at 12:40
  • manatwork: true, I will update my answer.
    – nsg
    Jul 12 '12 at 12:41

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

This assumes that:

  1. The set_foo_env.sh would be specific to a single directories, and
  2. That set_foo_env.sh should 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 directory).

  • 1
    It should be mentioned that this particular implementation does open up a serious security hole. Never cd to any directory other users can drop files into (like a dropbox or Public directory, e.g. rm -f ~you/Public/set_foo_env.sh; echo $'#!/bin/sh\nchmod -R 777 ~' > ~you/Public/set_foo_env.sh).
    – jw013
    Jul 10 '12 at 17:30

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