Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get GNU screen to read .bash_profile/.bash_rc changes?

After I make changes in .bash_rc or .bash_profile, when I start GNU screen, it doesn't recognize those changes. I can

source ~/.bash_profile 

and it works for the current screen window I have open, but I have to do that for every screen window I have open.

How do I get screen to read my latest changes in my bash configuration?

like image 464
Francis Lewis Avatar asked Aug 04 '11 17:08

Francis Lewis


People also ask

Should I put alias in bashrc or bash_profile?

bashrc or in ~/. bash_aliases but it should not go in . profile . If the variables are only ever needed in shells where the aliases (which expand to commands that use them) are available, then it is actually fine to put them in the same place as the aliases.

How does bash profile work?

The Bash profile is a file on your computer that Bash runs every time a new Bash session is created. This is useful because we need to run certain code every time before starting to work. OS X doesn't include a Bash profile by default, but if you already have one, it lives in your home directory with the name .


2 Answers

If you want screen to always treat your shell as a login shell, and source the same files that would be read if just started a new shell normally, add the following to ~/.screenrc (or maybe ~/.byobu/.screenrc, as pointed out in the comment):

shell -$SHELL 

This way, you don't need to manually tell it to source your files each time you start a new screen. Though you would have to if you just made changes and wanted those changes to be reflected in your current screen.

The documentation for this (and lots of other screen details) can be found here. Basically, shell is a command to screen telling it to run the following when it needs to create a new shell. $SHELL is the usual variable holding the path to your preferred shell. And the dash - in front of $SHELL indicates that it should be run as a login shell (which will typically mean it sources your ~/.bash_profile, etc.).

It's worth pointing out, however, that screen defaults to just inheriting most environment variables from the shell where you start screen; and a login sub-shell may alter some environment variables in unexpected ways. I ran into a situation where elements of my $PATH were basically permuted. I solved the problem thanks to this particularly excellent answer on superuser.

You may notice the source command available. It's important to note that this sources a file of screen commands, rather than shell commands. Other relevant (screen) commands include eval and exec.

like image 191
Mike Avatar answered Oct 14 '22 11:10

Mike


You have to do it in each screen that you have open since they are all different shells. If you need the change every time a new shell is opened, I suggest you put the changes in ~/.bashrc instead.

Apparently, you can send a command to all windows at once using this syntax:

C-a : at "#" stuff "source ~/.bash_profile^M" 
like image 22
gpojd Avatar answered Oct 14 '22 10:10

gpojd