Types of Linux Shell Sessions

Differentiate login, non-login, interactive and non-interactive 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:

blank
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

blank
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 :

blank
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.

blank
cat /etc/passwd

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

Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

RELATED POSTS

CronTab Basic Commands With Examples

Cron is a time-based job scheduler in Unix-like operating systems, which is used to automate repetitive tasks. It is a daemon (a background process)...

How To Change Your Default Linux Shell

The shell is a program that accepts and interprets commands. Besides bash, Linux supports many other shell programs, such as ksh, zsh, csh, and...

Â

RECENT 'HOW-TO'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string with a function or variable. It does not...

MORE ON CODEX

MORE IN THIS CATEGORY

CronTab Basic Commands With Examples

Cron is a time-based job scheduler in Unix-like operating systems, which is used to automate repetitive tasks. It is a daemon (a background process)...

How To Change Your Default Linux Shell

The shell is a program that accepts and interprets commands. Besides bash, Linux supports many other shell programs, such as ksh, zsh, csh, and...

CHECKOUT TUTORIALS

Java Tutorial #6 – Jump Statements

Introduction The break statement is used to stop further execution. It can be used either in a loop or within a switch statement. Break Statement The break...
- Advertisement -spot_img