Source new .bashrc in all open terminals

6

2

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?

Jeff

Posted 2013-03-22T17:02:50.090

Reputation: 207

Answers

6

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.

Dennis

Posted 2013-03-22T17:02:50.090

Reputation: 42 934

Much nicer! +1. – terdon – 2013-03-22T17:49:09.740

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 – 2013-03-22T20:25:21.527

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 – 2013-03-22T20:28:13.120

Read a file on every command? Meh. – Ярослав Рахматуллин – 2013-03-22T20:38:32.087

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 – 2013-03-22T21:13:54.570

I could think of one involving urxvt and perl. It's not like it matters, but in my opinion, running a command on every command and polling the filesystem (cache, right) is just bad form. – Ярослав Рахматуллин – 2013-03-22T21:52:08.337

2

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.

terdon

Posted 2013-03-22T17:02:50.090

Reputation: 45 216

0

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

Ярослав Рахматуллин

Posted 2013-03-22T17:02:50.090

Reputation: 9 076