I'm always wondering if there's a way to do this. I don't want to type source after changing my .bash_profile all the time.

  • Log back in (if typing source .bash_profile is too much trouble)...
    – jasonwryan
    Feb 8 '15 at 6:57
  • . ~/.bash_profile saves you a pair of keystrokes...
    – Mat
    Feb 8 '15 at 7:05
  • @jasonwryan I use alias now so I just want to know if auto source is possible...
    – laike9m
    Feb 8 '15 at 7:18
  • @laike9m I doubt it. Bash doesn't monitor the configuration files. You'll have to use an alias or function of some form.
    – muru
    Feb 8 '15 at 8:20

It is possible to make it happen automatically with a little work using PROMPT_COMMAND and -nt with a marker file. If you put this into your ~/.bash_profile file:

touch /tmp/bash_profile-$$
resource_profile() {
    [ ~/.bash_profile -nt /tmp/bash_profile-$$ ] && source ~/.bash_profile

then an empty marker file will be created in /tmp when the shell starts, named with the PID of the shell to identify it.

Before displaying the prompt every time the shell will run the resource_profile function, which will check whether ~/.bash_profile has changed (-nt) since the last time it was sourced and updated the marker file, and will automatically source ~/.bash_profile if it has changed. When the file is sourced it will update the marker file again for the next time.

This isn't foolproof - depending on the filesystem, changes made shortly after the file is sourced may not be detected. The file will only be checked after you run a command or press Enter, which is as close to immediate as it's possible to get, but not completely instantaneous.

Depending on the profile of your system you may prefer to put the marker file elsewhere where nobody else can access the directory. It's also possible to build something similar out of stat(1) if you have it, avoiding the marker file entirely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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