Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

The shell is a command line interpreter in Linux. It’s a program that receives commands from the user and gives them to the operating system to execute. Further, bash is the default login shell for most Linux distributions, but there are other Linux shells like the Korn shell (ksh), the Z shell (zsh), and the C shell (csh).

In this tutorial, we’ll learn the difference between interactive, non-interactive, login, and non-login shells.

2. Interactive Login Shell

An interactive shell receives commands from the user and displays output to the user. Moreover, users get a login shell when they login to their account.

We get an interactive login shell when we use programs like ssh or telnet:

$ ssh localhost
$ echo $-
himBHs
$ shopt login_shell
login_shell    	on

In the above snippet:

  • $- is a special bash parameter that returns the option flags used in the current bash shell. Further, if it includes i, it means that the shell is interactive
  • shopt login_shell tells us if the login_shell shell option is set

Thus, we have an interactive login shell.

3. Interactive Non-login Shell

We get an interactive non-login shell when we open up a terminal on our Linux machine:

$ echo $-
himBHs
$ shopt login_shell
login_shell    	off

We can see that we got an interactive, non-login shell.

4. Non-interactive Login Shell

If we give something other than tty (for example, a command) as stdin to ssh, we receive a non-interactive login shell:

$ echo 'echo $-; shopt login_shell' | ssh localhost
Pseudo-terminal will not be allocated because stdin is not a terminal.
hBs
login_shell    	on

In the above snippet, we passed ‘echo $-; shopt login_shell’ as stdin to ssh. As a result, we got a non-interactive login shell.

5. Non-interactive Non-login Shell

Finally, we get a non-interactive non-login shell when we run a script on our machine. That is because they run in their new shell that is non-interactive:

$ echo 'echo $-; shopt login_shell' > script.sh
$ chmod +x script.sh 
$ ./script.sh 
hBH
login_shell    	off

To clarify, we wrote ‘echo $-; shopt login_shell’ into script.sh and then made it executable using chmod. After that, we ran the script.

As we can see, we got a non-interactive non-login shell.

6. Conclusion

In this article, we learned about the different kinds of shells we can get in the Linux environment.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!