Difference Between a Login Shell and a Non-Login Shell

Fumbani Banda Jan 21, 2022
  1. What is a Shell in UNIX-Based Systems
  2. What is a Login Shell in UNIX-Based Systems
  3. What is a Non-Login Shell in UNIX-Based Systems
Difference Between a Login Shell and a Non-Login Shell

This tutorial explains the difference between a login shell and a non-login shell in UNIX-based systems.

What is a Shell in UNIX-Based Systems

A shell in UNIX-based systems is an interface between the user and the operating system’s kernel. It takes input from the user and sends it to the kernel, and it also takes the result from the kernel to the user.

There are two types of shells, the login shell and the non-login shell.

Every shell executes a series of start-up scripts to set up the environment once it is started. The scripts have different uses, and they all affect the environment.

Subsequent scripts can override the values that the previous scripts have set up.

What is a Login Shell in UNIX-Based Systems

A login shell is the first process that is started after successfully logging in using /bin/login by reading the /etc/passwd file. The login shell executes under your user ID.

A login shell is executed when you log in using the terminal, switch to another user, or use SSH.

Once a login shell is started, it executes a collection of start-up scripts to set up the shell environment. The following scripts are executed.

  1. The login process executes /etc/profile.
  2. /etc/profile executes the scripts in /etc/profile.d
  3. Login process executes ~/.bash_profile.
  4. ~/.bash_profile executes ~/.bashrc
  5. ~/.bashrc executes /etc/bashrc

The following command is used to indicate if the shell is a login shell or not.

echo  $0

Getting -bash or -su as the output indicates that the shell is a login shell. Take note of the - symbol preceding the output.

In the image below, the output of the echo $0 command has a - preceding the bash. It shows that the shell is a login shell.

login shell

What is a Non-Login Shell in UNIX-Based Systems

The login shell starts a non-login shell. It can be a shell that starts with a process without login or starts from another shell. A process uses the name of the shell executable to start a non-login shell.

Running the bash shell as a non-login shell executes the following scripts.

  1. Non-login process executes ~/.bashrc
  2. ~/.bashrc executes /etc/bashrc
  3. /etc/bashrc executes /etc/profile.d/

To determine if a shell is a non-login shell or not, run the following command.

echo $0

If the output is bash or su without a preceding - symbol, the shell is a non-login shell.

We execute the echo $0 command in the image below, and the output shows a - preceding the bash. It means that we are using a login shell.

To start a non-login shell, we type the name of the shell executable, bash, in our case. We execute the echo $0 command again to see what type of shell the newly started shell is; the output of the echo command is bash without the -. It means the new shell is a non-login shell.

nonlogin shell

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Linux Shell