Site icon Automation Dojos

Types of Linux Shell Sessions

(A) Login Shell

login shell is a shell where you login, either via the terminal or via SSH. If the user first logs in into a terminal session or SSH via authentication, such a 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.

Login Shell Startup Files

When you run a login shell it executes a number of files on start-up, first of all /etc/profile is sourced (executed in the current environment). As a second step following files are checked for existence and sourced if found in the following order:

  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

(B) Non-Login Shell

When the user who is already working on authenticated session starts a new shell session, a ‘non-login’ shell session is launched. There is no prior authentication required when the child session is started.

This mode describes opening a new terminal, for example, xterm or Gnome Terminal, and executing a shell in it or even opening a new shell session from within the existing login shell.

Non-Login Shell Startup Files

A non-login shell will read the following two files:

  • ~/etc/bashrc
  • ~/.bashrc

(C) Interactive Shell

When a shell session is attached to a terminal, it is called interactive. In other words when the commands are run with user-interaction e.g., from keyboard The key thing here is that the ~/.bashrc script gets executed behind the scenes every time this interactive shell is initialised

Interactive Shell Startup Files

In an interactive login shell the sourced files are:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

While in an interactive non-login shell following could be called:

  • /etc/bash.bashrc
  • ~/.bashrc

(D) Non-Interactive Shell

When a shell session is not attached to a terminal and probably run from an automated process it is called non-interactive and hence cannot interact with the user. It’s most often run from a script or similar automated means. The shell script runs in its own subshell and the shell only opens to execute the script and closes once the script has terminated.

Non-Interactive Shell Startup Files

In case of non-interactive non-login shell there is no interaction or login on behalf of the user, so the use case here is mostly automated scripts. In such cases, bash does not read any startup files except some of them may read the file in the $ENV variable

However there are odd or rare cases for the sake of argument where the script can emulate an interactive shell by prompting the user to input values. Sourced files in such cases could be:

  • /etc/bashrc
  • /etc/bash.bashrc

Bonus Tips: Common Commands

How To Check Shell Session

In order to find out if you are logged into your computer using a login shell, type the following echo command in your terminal:

echo $0

For a login shell, the output starts with a “-” followed by the name of the shell e.g -zsh or -bash. For non-login shell, it’s just the name of the shell e.g zsh or bash

List all Installed Shells

To see which shells are installed on your computer, use this command

cat /etc/shells

Verify User’s Current Shell

The $SHELL environment variable holds the name of your current shell. To confirm the current shell use :

echo $SHELL

Verify User’s Default Shell

You can also verify the users default shell by looking at the contents of the /etc/passwd file. A record contains the user’s account data, such as username, UID, GID, and home directory, among others. Generally the last field indicates the default shell of the user.

cat /etc/passwd

Note: Only the root user is allowed to write to /etc/passwd.

Exit mobile version