46

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?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
  • 1
    How about changing the accepted answer to another (correct) one, by Mike? – dotz Feb 01 '17 at 00:06
  • Actually, the accepted answer from gpojd is a good one. It's very useful if you're on a server as root to be able to do ```ctrl-a : at "#" stuff "source .gn/bashrc^M"``` and run your personal root bashrc (unalias rm cp mv to start with), and have all your windows source a personalised rcfile. Note ^M is the two characters caret and M. – Graham Nicholls Feb 01 '21 at 10:59

2 Answers2

74

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.

Community
  • 1
  • 1
Mike
  • 19,114
  • 12
  • 59
  • 91
17

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"
gpojd
  • 22,558
  • 8
  • 42
  • 71
  • I was able to source the .bash_profile for all open screen windows, but it's not working for any new screen windows I open up. It's also not loading all the new .bash_profile data when I start a new screen session. – Francis Lewis Aug 04 '11 at 17:30
  • 5
    I don't know why I put that. .bash_profile is only sourced the first time you log in. .bashrc is done each time a shell is opened. If you need it to happen each time, I suggest you put your changes in .bashrc and not .bash_profile. – gpojd Aug 04 '11 at 17:35
  • That worked perfectly! Thanks so much! I still would have thought putting stuff in .bash_profile would have loaded when I logged in, but that wasn't the case either. – Francis Lewis Aug 04 '11 at 17:42
  • Well, it's loaded into your login shell, but if screen was started before you made the changes, it won't take effect unless you completely close screen and start a new one [from a new login shell]. – Random832 Aug 04 '11 at 17:44
  • I had a huge .bash_profile and this [created problem for my WinSCP](http://winscp.net/eng/docs/message_large_packet) (and probably Filezilla). Solution below by @mike should be used. (`shell -$SHELL` in `~/.screenrc`) – laggingreflex Jan 21 '15 at 13:42