Source new .bashrc in all open terminals

bashbashrcterminal

I find myself having many terminals open at once all the time. When I update my .bashrc, I have to go to each terminal and execute

. .bashrc

to source the terminal with the new .bashrc.

This is kind of a pain and also time consuming. I was wondering if there is a way of sourcing all open terminals with the new .bashrc file without going to each one or logging out and starting over?

Best Answer

terdon's approach works well under the right circumstances, but if, e.g., .bashrc appends to the PATH variable, it will cause errors rather quickly.

Instead of simply resourcing the file, you could check its modification time first and compare it to the mtime of the last sourced version.

To do so, append this to ~/.bashrc:

  • Linux

    bashrc_sourced=$(stat -c %Y ~/.bashrc)
    
    PROMPT_COMMAND='
        test $(stat -c %Y ~/.bashrc) -ne $bashrc_sourced && source ~/.bashrc
    '
    
  • OS X and BSD

    bashrc_sourced=$(stat -f %m ~/.bashrc)
    
    PROMPT_COMMAND='
        test $(stat -f %m ~/.bashrc) -ne $bashrc_sourced && source ~/.bashrc
    '
    

Then, resource it manually one final time.

Related Question