What is the difference between Non-Login and Login, Non-Interactive and Interactive shell sessions in Linux/Ubuntu/Debian

In this short article, we shall provide a simple explanation of the differences between login and non-login shells as well as between interactive and non-interactive shells.

Background

Before delving into a clarification of these differences, some basic understanding of how shells are executed is required (we will limit this to bash).
When gnome terminal or any terminal emulator is opened, a non-login, interactive shell is executed.
An interactive, login shell is executed however, when the user logs into a computer using the command line or via ssh.
The user, when he logs in via GUI (.i.e graphically), is executing a completely different thing depending on the graphical environment and the system. It is the graphical shell in this case that deals with the login.
When a shell script is run, it is executed in a non-login, non-interactive shell.

The differences

Logging

Depending on how the session is launched, the bash shell reads several configuration files. One difference between distinct sessions is whether the shell is being invoked as non-login or as a login session.

1 – Login

If the user first logs in into a terminal session or via SSH, his shell session will be considered as a login shell. In other words, a login session is a shell session that starts by authenticating or identifying the user.

To tell that you are in a login shell, type in the command :

echo $0

This shows that you are not in a login sell. If the result comes up as -bash, then you are in a login shell. Here is an excerpt from man page :

Read: How to install GNOME Shell Extensions in Ubuntu

In Bash, you can also use shopt login_shell as shown below:

shopt login_shell

For instance in the terminal preferences (Ubuntu for instance), there is the option that enables to run a command as a login shell :

If you tick the option shown in the snapshot above, it means that every time you open up a new terminal window, it is going to run as a login shell.
This kind of session reads configuration data from the file /etc/profile first. Afterwards, It will search for the first login shell config file in the home directory of the user in order to get configuration details that are specific to the user.

It reads the first file among ~/.bash_profile, ~/.bash_login, and ~/.profile and does not proceed any further.

Read: How to set up a firewall on Ubuntu 22.04

2 – Non-login

When the user who is working on this authenticated session starts a new shell session, a non-login shell session is launched. Notice here that no prior authentication is required when the child session is started.

A non-login shell will read the file /etc/bash.bashrc and then the user specific file ~/.bashrc to create its environment.

Interactivity

When a shell session is not attached to a terminal, it is called non-interactive. When however a shell session is coupled to a terminal, it is an interactive session.

The environment variable BASH_ENV is read by Non-interactive shells which read the file specified in order to create the new environment.

Which shell is being used ?

In order to find which shell you are using, you can execute the echo command below :

echo “$ECHO”

This however does not tell which shell is running at the moment ($SHELL is the default login shell) but it rather shows the shell for the current user.

To find out exactly which shell you are using, run the ps command with -p {pid} switch as follows.

ps -p $$

This will select the processes whose ID numbers show up in {pid}. The $ returns the PID or process identification number of the current process which happen to be your shell. Invoking a ps command on $ will yield the desired result.

Read: How to fix high memory usage in Linux

How many shells are installed?

Pathnames of valid login shells can be found in the /etc/shells file. The cat command below will display all installed shells on your Linux :

cat /etc/shells

Conclusion

A script that is executed from the terminal is run in a non-login, non-interactive shell session whereas a session that is started with SSH for instance is an interactive login shell session.
A script that runs in its own non-interactive sub-shell that runs to execute another script and then closes immediately afterwards represents a Non-interactive non-login shell.
It is not common to encounter a non-interactive login shell, for instance, launch :

echo command | ssh server

When ssh is executed without a command, it starts a login shell. If the standard input (stdin) of the ssh is not a terminal(tty), it starts a non-interactive shell. This has some consequences on which files are accessed or read to initialize the shell session.

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

ziad nahdy

Ziad Nahdy, fan of open source and programming languages. He is a technical writer, blogger and Linux enthusiast. He loves to read and help others with their problems. He is addicted to open source software but he also loves other technology related subjects.

This Post Has One Comment

  1. Peter Teuben

    On macosx it reads bashrc_profie instead of bashrc, so they seem to be confusing the wheel type

Leave a Reply