Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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.

share|improve this answer
    
Much nicer! +1. –  terdon Mar 22 '13 at 17:49
    
Thanks so much! Do you need $HOME/.bashrc insted of just .bashrc? I would guess without $HOME, the terminal wouldn't be able to find it. –  Jeff Mar 22 '13 at 20:25
    
Source's arguments are filenames. If you're currently inside your home folder, . .bashrc will work; otherwise, it won't. You have to specify $HOME/.bashrc. ~/.bashrc or the full path (e.g., /home/jeff/.bashrc). –  Dennis Mar 22 '13 at 20:28
    
Read a file on every command? Meh. –  Ярослав Рахматуллин Mar 22 '13 at 20:38
1  
@Ярослав Рахматуллин: The file will be cached, so that's not a major problem. Short of using a terminal multiplexer, I can't think of a better way to meet the OP's requirement of not going to each terminal. –  Dennis Mar 22 '13 at 21:13

In general, please remember to include your Operating System, the correct answer is often system dependent. Remember that bash is used by default on most Linuxes, OS X and many UNIXes.

Anyway, in your case the OS should be irrelevant, so what you need to do is set the PROMPT_COMMAND variable:

Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.

So, since the command you want to run is sourcing ~/.bashrc, add this line to your ~/.bashrc (the . is just an alias to source):

PROMPT_COMMAND='source ~/.bashrc'

Now, every time Bash displays a prompt, it will first re-read ~/.bashrc. To get your open terminals (as long as they've been opened after you set PROMPT_COMMAND) to update just run any command or simply hit Enter.

WARNING: Depending on the complexity of your ~/.bashrc, this could add a noticeable lag since any commands in the file will be executed repeatedly.

share|improve this answer

KISS

Put either or both of these in either or both of /etc/profile and ~/.bashrc

alias sProf='source /etc/profile'
alias sBrc='source ~/.bashrc'

another approach:

Copy a terminal line including everything between the arrows to include the command and the newline and the middle-click it into every terminal.

  <---------------------------->
$ source ~/.bashrc             | terminal border
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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