Configuring GNU Screen to Source .bash_profile Instead of .bashrc for New Windows


1 views

When working with GNU Screen, you'll notice that new windows automatically source ~/.bashrc but not ~/.bash_profile. This behavior stems from how bash handles interactive non-login shells versus login shells:

# Typical shell initialization flow:
# Login shell: /etc/profile → ~/.bash_profile → ~/.bashrc
# Non-login shell: ~/.bashrc

Many developers maintain critical environment variables in .bash_profile while keeping shell-specific configurations in .bashrc. When these variables don't propagate to new screen windows, it can cause:

  • Broken PATH configurations
  • Missing application-specific variables
  • Inconsistent environment between terminals

The cleanest approach is to modify your ~/.screenrc file:

# Force bash to run as login shell in new windows
shell -$SHELL
defshell -$SHELL

This tells screen to spawn new windows as login shells, which will automatically source .bash_profile.

For more control, create a wrapper script:

#!/bin/bash
# ~/bin/screen_shell
exec bash --login -c "$*"

Then configure screen to use it:

# In ~/.screenrc
shell /home/username/bin/screen_shell

To verify your solution works, create a test window and check:

echo $0
# Should show "-bash" indicating login shell

If issues persist, add debugging to your bash files:

# In ~/.bash_profile
echo "Sourcing .bash_profile" >&2

For system-wide configuration (affecting all users):

# In /etc/screenrc
shell -/bin/bash

Remember that this change will affect how all programs launched from screen inherit environment variables.


When working with GNU Screen, you might notice that new windows don't inherit your environment variables as expected. The root cause lies in how different shells handle initialization files:

# Typical bash initialization sequence:
1. Login shell: /etc/profile → ~/.bash_profile → ~/.bashrc
2. Non-login shell: ~/.bashrc

GNU Screen creates non-login shells by default, which means:

  • Only .bashrc gets sourced
  • Environment variables set in .bash_profile are missing
  • Path modifications and other crucial settings don't propagate

Add this to your ~/.screenrc to force login shells:

# Force bash as login shell
shell -$SHELL

# Alternative: Specific path to bash
# shell -/bin/bash

Create a test script to verify your setup:

#!/bin/bash
echo "Testing environment variables:"
echo "PATH: $PATH"
echo "Custom Variable: $MY_VAR"

For cases where you can't modify .screenrc:

# In your ~/.bashrc
if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

Or launch screen with:

screen -c /dev/null -- bash -l
  • Check shell inheritance: echo $SHELL
  • Verify file permissions: ls -la ~/.bash_profile
  • Test without screen: bash -l -c "env"