Categories
Linux Tutorial Series

Linux Tutorial Series – 84 – Interactive vs non-interactive and login vs non-login shells

Here is the video version, if you prefer it:

Today we will talk about two properties (or classifications) of a shell. The first is whether a shell is interactive or non-interactive and the second is whether the shell is login or non-login.

Let me preface this by saying that I was hesitant about going into this topic. I was thinking like this: “Is this really essential for someone learning about Linux?”. I concluded that it is. Why? Because these different shell properties do pop up from time to time and I think that they are important because they help you understand how Linux handles shell-related things (such as establishing the environment, for example).

Before we begin, let’s remind ourselves what a shell is. A shell is a program that runs commands. (Ward, 2014)⁠ I (and most likely, you) am using bash (which stands for Bourne-again shell). You can have multiple instances of shell running on your computer. Just open up two or more Terminal instances – each one of those is a separate shell. (“Is each terminal window of Bash a separate shell?,” n.d.)⁠

Interactive shells expect the user to be able to interact with the shell (as in the user enters some input to the shell via keyboard). Non-interactive shell assumes that shell is probably run from an automated process and it can’t expect interactivity, nor can it expect that anyone will see its output; its output will most likely be written to a file. (“What is the difference between interactive shells, login shells, non-login shell and their use cases?,” n.d.) This makes sense – you will not always want to interact with your shell via keyboard or you will want to have your shell running without you being present, so there are these two types.⁠

In order to explain the difference between login and non-login shells, let me first explain why do these two types exist. Login shells exist to do some things only once (such as set your HOME variable, which contains the path to your users home directory). (“What is the intended purpose of a login shell?,” n.d.)⁠ How do login shells do these things, exactly? By reading certain files. Non-login shells are all the shells that don’t read the certain files that login shells read. We will talk more about these files in another post, but for now just understand this:

  • Interactive shells exist for when you want to interact with your shell (via keyboard)
  • Non-interactive shells exist for when you don’t want to interact with your shell (you want it to run without your supervision)
  • Login shells exist to do some things only once (i.e. set your HOME variable and run some things which should only be run once)
  • Non-login shells exist for all other usages after the login shell has done all of the things that have to be done once

Every shell can be either value on both of the dimensions. That means that a shell can be:

  • login and interactive
  • login and non-interactive
  • non-login and interactive
  • non-login and non-interactive

This needn’t concern you that much. Just know that every shell that is run via Terminal is an interactive, non-login shell.

I think that this is all you need to know. An optional reading which explains in more detail the usage of all 4 types of shell listed above in the list is (“Why do we have login, non-login, interactive, and non-interactive bash shells?,” n.d.)⁠

Hope you found this useful! As I mentioned at the beginning, this will be necessary for some later understandings.

References

Is each terminal window of Bash a separate shell? (n.d.). Retrieved February 7, 2020, from https://www.quora.com/Is-each-terminal-window-of-Bash-a-separate-shell

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 292-294

What is the difference between interactive shells, login shells, non-login shell and their use cases? (n.d.). Retrieved February 7, 2020, from https://unix.stackexchange.com/questions/50665/what-is-the-difference-between-interactive-shells-login-shells-non-login-shell

What is the intended purpose of a login shell? (n.d.). Retrieved February 7, 2020, from https://unix.stackexchange.com/questions/435964/what-is-the-intended-purpose-of-a-login-shell

Why do we have login, non-login, interactive, and non-interactive bash shells? (n.d.). Retrieved February 7, 2020, from https://superuser.com/questions/657848/why-do-we-have-login-non-login-interactive-and-non-interactive-bash-shells

NewsletterUpdates on interesting things I am doing

Subscribe to my newsletter to keep abreast of the interesting things I'm doing. I will send you the newsletter only when there is something interesting. This means 0% spam, 100% interesting content.

Leave a Reply

Your email address will not be published.