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.
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.527Source'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.120Read 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