Ubuntu Desktop: Why the .profile script is executed in an interactive non-login shell?
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:
- Why is my ~/.profile being executed on opening a terminal?
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)
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.