How 2 run same command across all open terminals

Hi folks. This has been bothering me for a while.

Among the 8 virtual desktops I'm using, I have 18 terminals open right now.

I change some of my user configuration (e.g. put a new alias into ~/.bashrc); but in order to use this new added alias, I have to source the config file:

. ~/.bashrc

That's all good, but the sourcing stays local to the current terminal. Is there a way to source my .bashrc on ALL open terminals, so that I can use it from any one of them?

I know I can echo into any terminal

echo Hello > /dev/pts/15

But is there a way to run a command like this?
Something like

$ echo . ~/.basrhc | /dev/pts/15
bash: /dev/pts/15: Permission denied
bash: echo: write error: Broken pipe

I'm running bash on CentOS box.
Thank you!

It seems that every shell you run is a login shell. Bash reads and executes the ~/.bashrc file only if it is started as an interactive, but not login shell. When Bash starts as an interactive login shell, it reads and executes the ~/.bash_profile file. What you could do is to source the ~/.bashrc file from the ~/.bash_profile file. Something like this:

# .bash_profile
# sourcing .bashrc:

. ~/.bashrc

this way every interactive shell you start will read and execute the ~/.bashrc file, be it a loging shell or not.

Thanks for the response.
But that is not my problem. I'd like to source the config file (.bashrc, .bash_profile, .myConfigFile, etc.) across all terminals that are already running. So that I don't have to close them, and restart (and lose history, kill programs tied to them etc.)

To my knowledge you'll have to manually source the rc file in each running instance of the shell.

If you want to be clever, and anticipate doing this often enough, you could put the following command into your .bashrc file:

trap ". $HOME/.bashrc" 16

Once that is set in a running instance of bash, you then only need to send a SIGUSR1 (16) to the process and it will resource the .bashrc file. You could write a small script that susses out all of your running login/interactive shells and executes the necessary kill -16 command. You could pick another signal, but this seems just fine for this case.

Hope this helps some.

2 Likes

Thanks agama,

That's a slick solution there with SIGUSR1, that can be certainly put to use.

I also have had the feeling it cannot be done straightforward.