I'm trying to differentiate these four terms login, non-login, interactive and non-interactive:
- interactive - login shell
- interactive - non-login shell
- non-interactive - login shell
- non-interactive - non-login shell
As I understand
interactive - non login shell: Start the system, log in to system and open terminal and
non-interactive - login shell: telnet to system and log in
But what about an interactive - login shell?
Does it log in to the system, open virtual terminal and log in? and
non-interactive - non-login shell, is it running automated script in crontab?
The only real misconception you seem to have is about what constitutes a non-interactive, login shell.
Briefly (see here for more details), with examples:
interactive login shell: You log into a remote computer via, for example
ssh
. Alternatively, you drop to a tty on your local machine (Ctrl+Alt+F1) and log in there.interactive non-login shell: Open a new terminal.
non-interactive non-login shell: Run a script. All scripts run in their own subshell and this shell is not interactive. It only opens to execute the script and closes immediately once the script is finished.
non-interactive login shell: This is extremely rare, and you're unlikey to encounter it. One way of launching one is
echo command | ssh server
. Whenssh
is launched without a command (sossh
instead ofssh command
which will runcommand
on the remote shell) it starts a login shell. If thestdin
of thessh
is not a tty, it starts a non-interactive shell. This is whyecho command | ssh server
will launch a non-interactive login shell. You can also start one withbash -l -c command
.If you want to play around with this, you can test for the various types of shell as follows:
Is this shell interactive?
Check the contents of the
$-
variable. For interactive shells, it will includei
:Is this a login shell?
There is no portable way of checking this but, for bash, you can check if the
login_shell
option is set:Putting all this together, here's one of each possible type of shell:
Essentially, whether a shell is login or not, interactive or not matters for exactly one reason:
The initialisation files and default options set depend on whether a shell is login or not and interactive or not.
Correspondingly, whether a shell is login or not or interactive or not depends solely on the invocation used - the exact command name and options.
The two properties are otherwise orthogonal - whether a shell is login or not has no bearing on determining whether it's interactive or not.
Bash starts a login shell if any of these are true:
argv[0]
, the name of the command it was invoked as, starts with a-
-l
option is specifiedSimilar, bash starts an interactive shell if any of these are true:
bash some/file
) or a command string to run (bash -c 'foo'
) (the actual condition is a bit more complex, see the manual)-i
option was specifiedNotably (and paradoxically), the latter implies that
bash -ic 'foo'
starts an interactive shell.So the following starts a login, interactive shell, even though it has nothing whatsoever interactive about it and the invocation had nothing to do with logging in:
That logging in via console or GUI starts a login shell (or maybe not) is entirely an effect of the login process using the appropriate invocation.
The conditions and effects are described in detail in the bash manual, section on Startup Files.
A major source of confusion is that there is another common meaning for "login" shell:
A user's login shell is the shell defined in that user's
passwd
entry (which may come from/etc/passwd
, LDAP or some other source).The
login
program, SSH, etc. start this shell as a login shell in the sense meant in the rest of the answer - with a leading-
in the command name, usually. If you wanted to be particularly confusing, you could say:Some login processes start the login shell of the user as a login shell.
Note that GUI login starts a login shell purely because the developers thought it convenient - LightDM runs a script on login which obviously isn't interactive and certainly doesn't depend on the user's login shell (in the second sense). Do not depend on the display manager starting a login shell, though - not all of them do, and on Wayland and GNOME, the login process doesn't use shell scripts at all.
Login shell:
Interactive shell:
Simply put: Interactive shell require user input, while non-interactive shell are run by scripts and don't require user inputs.
One category of shell (Non-interactive, login shell with hBc options) is missing in the accepted answer. It is commonly used unlike the one with hBs options to execute a command in a ssh session. To see, compare 4 and 5. I included it with others.
What is a login Shell?
It reads and executes commands from the first available file (if exists) during login session in the following order:
It reads and executes commands from all files (if exists) during logout in the following order:
Note: The difference in calling login files (only the first available file) and logout files (all files).
Note: A confusion always comes up when
~/.bashrc
file commands are executed during login shell session. Generally, commands in~/.bashrc
file should not be executed during login shell. However, if it occurs, it is because of~/.bash_profile
file includes~/.bashrc
file in it. Verify your~/.bash_profile
file. This is a common behavior in Fedora based linux distributions.What is a no-login shell?
It reads and executes commands from user-specific settings file: ~/.bashrc
What is a non-interactive session?
It reads and executes commands from
BASH_ENV
variable. It is also a way to control the behavior of non-interactive session.Note:
BASH_ENV
must contain full-path of shell-script file.Note: ssh remote non-interactive shell session does not read
BASH_ENV
. Instead, it reads~/.bashrc
file of remote user in remote system.Note:
ssh
remote sessions do not execute commands from files mentioned in login shell (~/.bash_profile).This will not work:
This will work: Because,
.bashrc
belongs to current user who is assumed as remote user, and remote system is assumed as localhost in this example.How to control the behavior of login shell?
using
--noprofile
option to disable reading commands from files mentioned above in login shell.How to control the behavior of no-login shell?
--rcfile
option to specify user-specific rc file--norc
option to disable reading commands from files mentioned above in no-login shellI'd like to mention, that you can start an interactive login shell by:
sudo /bin/login
and typing in your credentialsexec -l /bin/bash
su -
Also, you can check (in bash) if it the shell is login by typing
echo $0
and if the output starts with a dash-
, then it is a login shell.