4

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).

Franklin Yu
  • 1,237
  • 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 09:48
  • 1
    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. – RichieHH Oct 18 '19 at 21:30
  • 1
    @RichieHH Yes, I also got hit by different behavior between desktop managers. LightDM, unlike GDM, doesn’t source .profile in Debian. – Franklin Yu Apr 21 '20 at 04:58
  • update: It doesn't read .profile if your default shell is zsh using wayland.. (man chsh). – RichieHH Aug 07 '20 at 14:54

2 Answers2

7

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).

tripleee
  • 7,699
  • 2
    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
  • A login shell is one whose $0 begins with a dash, i.e. -sh. https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell/46856#46856 – tripleee Apr 23 '17 at 12:29
0

You can source the .profile from your .zprofile using ZSH emulation to deal with incompatibilities between bash and zsh.

Add the folloging to your .zprofile:

  • If you use a recent zsh version:

    # Load .profile (but emulating sh to avoid bash/zsh incompatibilities)
    emulate sh -c 'source ~/.profile'
    
  • If you're using an older zsh version:

    emulate sh
    . ~/.profile
    emulate zsh
    

Reference: Zsh not hitting ~/.profile

AdminBee
  • 22,803