Whenever you operate a Linux system behind the scenes, you might be encountering the interactive login or non-login shell.
The most important thing is that you are not aware of them, so you donโt know when they are switching, and this lack of knowledge makes it hard for young Linux users to understand the concept of Linux profiles like โ/etc/profileโ, โ~/.bash_profileโ, โ~/.profileโ, โ~/.bashrcโ, etc.
Today, all doubts related to interactive vs. non-interactive and login vs. non-login shells will be cleared.
Starting with,
Interactive vs Non-Interactive Shell
To understand the difference between them, you need to understand that in both cases, a shell is being used, so the shell is in the center for both of them.
Now, an โinteractive shellโ is when the user (referring to you) directly interacts with the shell by typing the commands in the terminal, where the shell will read your commands and return you the processed output.
Whereas, a โnon-interactive shellโ is a method of working with shell without user interaction, such as a shell script.
Note that every shell script uses the non-interactive shell approach and does not load or execute any startup file; instead, it inherits the environment variables from the shell that created it.
Thatโs all you need to know about them; letโs move on to the next.
Login vs Non-Login Shell
An interactive shell can either be a login shell or a non-login shell.
The โinteractive login shellโ is the first process that initiates under the user ID after system boot, or using the โ-login
โ flag. Even if you log into a remote system using SSH, it will be considered an interactive login shell.
Apart from that, you must also know that interactive login-shell will load some configuration files (or profiles), like โ~/.bash_profile
โ, into your current shell environment.
This file will be sourced (read and executed) in your current shell environment only once, when you boot your system; after that, you most probably interact with an interactive non-login shell.
The โinteractive non-login shellโ is when you initiate multiple shells in your current environment by executing bash in your terminal or opening a new terminal tab. So you are able to get any number of them after you log in to the system.
This event will not load the profiles like โ~/.bash_profile
โ, but instead source (read and execute) the โ~/.bashrc
โ file.
So, the major difference between both of them is that the interactive login shell will initiate on system boot and load the โ~/.bash_profile
โ only once.
After that, you only interact with the interactive non-login shell, and it will not load the โ~/.bash_profile
โ; instead, it will load the โ~/.bashrc
โ, a number of times depending on the number of times you initiate a new interactive non-login shell.
One more thing you must know is that many distributions use โ~/.profile
โ instead of the โ~/.bash_profile
โ file.
So, when an interactive login shell is initiated, it will first look for the โ~/.bash_profile
โ file; if the file does not exist, it will look for the โ~/.profile
โ file; if that file also does not exist, it will look for the โ~/.bash_login
โ file.
Note that if any of these files (โ~/.bash_profile
โ, โ~/.profile
โ, โ~/.bash_login
โ) do not exist in your home directory, you can manually create them using your choice of text editor.
However, in an interactive, non-login shell, it will only look for the existence of the โ~/.bashrc
โ file and will load the environment variables and customizations in your current shell environment.
Thatโs all you need to know; going more in depth might make you more confused on these simple terms.
If you have any suggestions that must be included in this article, then let us know in the comment section.
Hi, does the interactive non-login shell has a loaded/sourced โbaseโ (โ~/.bash_profileโ, โ~/.profile)?
(I understand it doesnโt load it every time new non-login shell is opened, but does it โcontainโ what was there during the boot?).
Or it sources ONLY .bashrc so everything in the โ~/.bash_profileโ, โ~/.profileโ is completely ignored?
Interactive non-login shells do not directly source ~/.bash_profile or ~/.profile. However, they inherit environment variables and settings from these files indirectly through a chain of sourcing.