2021-03-04

.bashrc, .profile, .bash_profile in Lubuntu vs Debian LXDE

Here we go again... profile vs bash_profile vs bashrc dot files.

In Lubuntu

I had figured previously [1] that:

  • Desktop environment's graphical login sources .profile
  • bash started in login mode sources .bash_profile, but if that doesn't exist, then falls back to sourcing .profile
  • bash started interactively (hence in non-login, or interactive, mode) sources in .bashrc only
  • Both .bash_profile and .profile should contain code to source in .bashrc

So when does code in each file run?

.profile has code you want to run only once, and only upon your DE graphical login.

.bash_profile has code you want to run once only upon your bash shell executing in login mode

.bashrc has code you want to run every single time bash starts, or when you login (if .profile sources in .bashrc as it should), or when bash starts in login mode (if .bash_profile sources in .bashrc as it should).

In Debian LXDE

It seems that:

  • Desktop environment's graphical login AND bash started in login mode both source .bash_profile, but if that doesn't exist, then falls back to sourcing .profile
  • bash started interactively (hence in non-login, or interactive, mode) sources in .bashrc only
  • .bash_profile or .profile should contain code to source in .bashrc
  • You should not have both .bash_profile and .profile since then the latter will be ignored anyway

This meant there's no place to put code that you want to run only once, and only upon your DE graphical login.

The workaround is to put the code in .profile (and you shouldn't have .bash_profile around as it'll override .profile).

Then make that code run only if the GUI is running, which you can detect.  And also make the code only once only, by checking if it's run before.

e.g. in .profile


if [ -n "$DISPLAY" ]; then
    if [ ! -n "$RUNONCE" ]; then
        export RUNONCE="done" # like a C include guard
        cat "GUI running" > GUI_is_running.txt
    fi
fi 

In a console without a GUI, $DISPLAY is not defined.

To learn more

 Some resources from other sites:

[1] profile bash_profile bashrc on Ubuntu Linux, Macs, and Windows

No comments: