I recently switched from terminal prompt login (getty?) to GNOME Display Manager. It seems that GDM always reads .profile, regardless of user's setting of login shell (Zsh in my case). Why is that? I assume it's hardcoded in their source, but I can't find. Why did they do that? Does the software depends on some functionality of Bourne shell?

This is not very good if I want to use both GDM and getty (as fallback), because I then need to keep my .profile and .zprofile in sync. I'm not so confident about sourcing .profile in .zprofile (I met some compatibility issues before, when I tried to source .bashrc in .zshrc). I think Bash called as /bin/sh behaves in POSIX mode, but I'm not sure whether it avoids all the pitfalls.

In case it matters, I'm on latest Arch Linux, running GNOME with Wayland (so there should not be any Xsession script involved).

  • Not sure what "avoids all the pitfalls" is supposed to mean. But indeed, not all Bash features are completely disabled even when you run Bash in POSIX mode (i.e. as /bin/sh). – tripleee Apr 23 '17 at 9:48
  • The pitfalls are multiple repeats of the same paths on $PATH so so forth. As a late note the OP is correct in pointing out, indirectly, the mess that is Linux start up files at GUI/ssh/shell"X" etc start. The debian startup files documentation, for example, clearly states that .profile is not sourced. Logging into Gnome via GDM3 however, it absolutely is. – HörmannHH Oct 18 at 21:30

Your problems with .bashrc are unrelated. .profile needs to be compatible with all sh-compatible shells, whereas of course .bashrc is specific to Bash and should generally not be sourced by other shells.

Generally, put the stuff you want to share between shells in .profile, and make sure you do source it from the startup files of your other shells (unless of course they already do that by default).

Obviously, you need to make sure you avoid code which behaves differently in different shells (lack of quoting is okay in Zsh but a problem in properly Bourne-compatible shells, for example).

As for the "why" part of your question, this is so that settings in your .profile are available to programs you run from your GUI session, not just by the ones you run from within a shell (or maybe we should say "traditional" shell, and regard your GUI session as a "non-traditional" shell).

  • I found that .profile is sourced explicitly in /etc/gdm/Xsession. The .profile seems to be read only once, so I guess the real login shell is neither /bin/sh (otherwise it'll be read twice) nor Zsh (otherwise .zprofile should be read), but /etc/gdm/Xsession or some other GDM program. The meaning of "user login shell" seems to have changed in GDM; that setting no longer changes the login shell (because that is already specified in GDM login screen), but changes the behavior of GNOME Terminal. Is that right? – Franklin Yu Apr 23 '17 at 10:17
  • In addition, I test POSIX conformance with Dash. Is that the preferred way? – Franklin Yu Apr 23 '17 at 10:22
  • Last time I looked, Xsession was a sh script. It's not an interactive login script by itself so it needs to read .profile explicitly. – tripleee Apr 23 '17 at 10:34
  • Dash is a good POSIX test, yes. – tripleee Apr 23 '17 at 10:34
  • By the way, how does the shell know whether it's a login shell? – Franklin Yu Apr 23 '17 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.