Score:5

Ubuntu Desktop: Why the .profile script is executed in an interactive non-login shell?

kz flag

I understand about the difference between Interactive login and Interactive non-login

According with this excellent post: Zsh/Bash startup files loading order (.bashrc, .zshrc etc.) we have the following table

+----------------+-----------+-----------+------+
|                |Interactive|Interactive|Script|
|                |login      |non-login  |      |
+----------------+-----------+-----------+------+
|/etc/profile    |   A       |           |      |
+----------------+-----------+-----------+------+
|/etc/bash.bashrc|           |    A      |      |
+----------------+-----------+-----------+------+
|~/.bashrc       |           |    B      |      |
+----------------+-----------+-----------+------+
|~/.bash_profile |   B1      |           |      |
+----------------+-----------+-----------+------+
|~/.bash_login   |   B2      |           |      |
+----------------+-----------+-----------+------+
|~/.profile      |   B3      |           |      |
+----------------+-----------+-----------+------+
|BASH_ENV        |           |           |  A   |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|~/.bash_logout  |    C      |           |      |
+----------------+-----------+-----------+------+

The first link refers to this other excellent post: Cleaning up bash customizations where has available an excellent explanation about Interactive login and Interactive non-login as follows:

An interactive login shell is a shell that you are typing into, that is the first such shell you execute on the machine. Typically you will have had to log in immediately before the shell starts. For example, when you SSH to a remote system and type commands to that system, you are typing into an interactive login shell.

An interactive non-login shell is a new shell started once you have already logged in; one which doesn’t require that you log in again. For example, if you open a new terminal window in your graphical user interface and get a shell prompt, that’s an interactive non-login shell. Another example of an interactive non-login shell would be a sub-shell started from inside a text editor; for example, typing :sh in vi.

About the former, it practically applies mandatorily in Ubuntu Server environment - where to work with each tty is need it do a login, so according with the table ~/.profile is used (I confirmed does not exist neither the ~/.bash_profile nor ~/.bash_login files). It applies for su - otheruser (it asks for his/her password) and therefore his/her own ~/.profile file is executed too. Until here I am fine.

About the latter - reason of this post - Ubuntu Desktop environment, it applies when a new window/terminal is opened with just one tab by default, so according with the table should be executed the /etc/bash.bashrc and ~/.bashrc files and not the .profile file

But why if is executed in that terminal:

  • echo $JAVA_HOME (defined on .profile)
  • echo $M2_HOME (defined on .profile)
  • echo $GRADLE_HOME (defined on .profile)
  • which java
  • which mvn
  • which gradle

all the commands work normally?

Same behavior for any new tab (Ctrl + Shift + T) within the same Window and even for a new Terminal Window with again its unique default tab

I read the following post:

And I don't have the settings mentioned about (the second is adapted for Ubuntu Desktop 18:04):

  • The .bashrc files does not refer or sourcing the .profile file
  • Edit --> Preferences --> Unamed -> Command tab --> Run command as login shell (it is unchecked)
raj avatar
cn flag
raj
It is not the shell that is executing the `.profile` file. Try to insert a command like `echo This is .profile` into the `.profile` file - you won't see the message on any new terminal you open, and you **should** see it if the shell were actually executing the file. It is your **desktop environment** (GNOME or other) that executes this file on session startup. The environment variables defined there are then inherited (as said in the answer below) by **all processes** started from the desktop environment, **including terminals**.
Manuel Jordan avatar
kz flag
@raj Does that logic break/violate the rules indicated in the table(s) shared in the two links on my question? At a first glance seems yes - that behavior happens in other Linux distributions?
raj avatar
cn flag
raj
No, it does not. The logic refers to **executing startup files by the shell**. The `~/.profile` file is **not executed by the shell** in this case. So everything is OK. Nothing is said above about executing the file by **other** software than shell. Also, as said in the first comment to the answer, it is crucial that the file should be executed by the DE upon login, because otherwise bad things will happen in terminals started from DE (for example you wouldn't have `$PATH` defined correctly, so some commands that work when you login over ssh will not work from DE).
Score:8
cn flag

Scripts that are executed for a login shell (systemwide /etc/profile, any script in /etc/profile.d, your local ~/.profile and the other files you list) define the environment of your current user - since you logged in.

Any non-login shell that you subsequently open, will at least inherit the environment of your login shell. That is why you (already) have all your environment variables defined in .profile when you open a new terminal.

cc flag
Exactly, inheritance is the key. Unfortunately, decades ago, the CDE (Common Desktop Environment) folks didn't get the memo, and terminals popped up from their desktop (and same problem today for many DE) never get your environment because 1)not a login term, and 2)forked from a not-your-process, so inheritance doesn't work. Resulting in so much crap being dumped into the .bashrc. Horrible since that stuff is done every command (run set -xv and see what bash is actually having to execute)
Manuel Jordan avatar
kz flag
@vanadium Even when your answer is correct - does that logic break/violate the rules indicated in the table(s) shared in the two links on my question? At a first glance seems yes - that behavior happens in other Linux distributions?
vanadium avatar
cn flag
According to the flow chart, `profile`-type files are executed on log in, whether you log in on a TTY (Interactive login shell) or in a desktop environment (non interactive login shell). Also .bashrc is run as well, because it is sourced by profile. After login, when you open a new terminal or a subshell (e.g. in TTY) (interactive non login shells), only bashrc-type files are run.
raj avatar
cn flag
raj
@vanadium Actually, based from the logs in `/var/log/syslog` regarding GNOME session startup, you can tell that `/etc/profile` and `~/.profile` files (but **not** `.bash_profile` nor `.bash_login`) are explicitly sourced by script `/etc/gdm3/Xsession` which is run by `gdm-x-session` which is started directly by gdm. After this, `dbus-update-activation-environment` is called by `gdm-x-session` to export environment to other processes in the session (started separately by systemd) over D-Bus. So this is quite complicated, but no login shell is involved anywhere.
vanadium avatar
cn flag
@ray yes, the specifics may be way more complex indeed. Question here was "why .profile was executed in a non-login shell" - simple answer is - "no, it is *not* being executed. It, however, was already executed before so you inherit its settings in your non-login shells." .
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.