13

I know what the difference between interactive/non-interactive and login/non-login shells are, but it seems like in practice there's never going to be a non-interactive login shell unless you have something like /bin/bash --login some-script.sh in a script (and even that seems a little odd). Is this correct or are they more common?

  • 7
    telnet towel.blinkenlights.nl for a login shell that is non-interactive ;-) – Martin Tournoij Feb 3 '16 at 16:21
  • 1
    This question is answered briefly within this longer answer. – Wildcard Jan 23 '17 at 12:05
  • @CaptainMan: Looking at the answers, we have two that are focused on quite different meanings of "interactive." In light of this can you expand your answer a bit to explain which meaning you were using when you asked the question? – Curt J. Sampson Oct 15 '17 at 0:58
8

I assume you're talking about Bash's concept of login vs. non-login and interactive vs. non-interactive Bash shells as described in the Invocation section of the manpage. (This is different from the interpretation in James Youngman's answer of any arbitrary command used as the "shell" (or user command interpreter) in the passwd(5) file and whether or not that program accepts user input; some, such as /usr/sbin/nologin, obviously don't.)

You are correct that /bin/bash --login some-script.sh will produce a non-interactive login Bash invocation, and this is perhaps a pathological example. There is one case, perhaps uncommon but not truly weird, that produces a non-interactive login shell: ssh somehost < some-file. Here sshd will start Bash with argv[0] set to -bash because it's not been given a command to run, causing Bash to consider itself a login shell, but, because stdin is not connected to a terminal, Bash will not set itself to interactive mode ($- will not contain i).

(Personally, that case seems far more reasonable than the converse, ssh somehost somecommand, which is not considered a "login shell" even though it's a fresh login to somehost just as the above is.)

I have recently done what I should have done long ago and put together a table of Bash's modes and what init files are run. If you're finding it confusing, take heart in that at least I do as well. It mystifies me what their original aim was with the rules about when .bashrc is executed.

1

Most login shells by count on a newly installed system are non-interactive, actually:

$ awk -F:  '{print $7}' <  /etc/passwd | sort | uniq -c
  5 /bin/bash
 23 /bin/false
  1 /bin/sh
  1 /bin/sync
 17 /usr/sbin/nologin

Clearly /bin/bash and /bin/sh are traditional shells and they're interactive. But all the other items in that list are non-interactive. If you're reading the list and don't know what one of them does, you can just look up its manual page (with for example man nologin or man sync).

What the command actually does

Looking at the Captain Man's posting history I see that they're quite inexperienced with Unix. So maybe the question in the comment about not following related to the command line at the top of the answer, not simply the output. So I'll explain the command-line too, even though it's really off-topic for this question.

The command is a Unix pipeline. A pipeline is a chain of commands - you read it left-to-right - in which the output of the first command becomes the input of the second, the output of the second becomes the input of the third, and so on, until the end of the pipeline. The output of the last process is shown on the terminal (unless it's been redirected). See the Wikipedia entry on shell pipelines for more information.

If you don't understand what a pipeline is doing, you can simply run it in segments to see what's happening. You can also read the manual page for the commands which are being used (here, awk, sort and uniq). In fact, you should do that now. I'll wait.

Let's run the stages of the pipeline incrementally (you can safely do this on your own Unix system):

~$ awk -F:  '{print $7}' <  /etc/passwd | sed -e 's/^/    /'
/bin/bash
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/bin/sync
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/bin/false
/bin/false
/bin/false
/bin/false
/bin/false
/bin/false
/bin/false
/bin/false
/bin/false
/usr/sbin/nologin
[ ... and so on, I've left the rest out ... ]

The output above is simply the contents of the seventh field from the /etc/passwd file. That's the (flat-text-file) database which tells the system what everybody's login shell is. If you want to find out more about /etc/passwd just go read it (it's world-readable) and look at the manual page for it (man 5 passwd).

So reading the whole list there you can get an idea what the popular items are, but that's not a good format for an answer to this question, because the question was really about how common non-interactive shells are. Let's count them up. The simplest way to do that is to sort the items first:

~$ awk -F:  '{print $7}' <  /etc/passwd | sort
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/false
/bin/false
/bin/false
/bin/false
/bin/false
[ ... and so on, I've left the rest out ... ]

We can use the program uniq to show us only the unique items:

~$ awk -F: '{print $7}' < /etc/passwd | sort | uniq | sed -e 's/^/ /' /bin/bash /bin/false /bin/sh /bin/sync /usr/sbin/nologin

But wait, that's no use, how many of each were there? Let's ask uniq (read the man page!):

~$ awk -F:  '{print $7}' <  /etc/passwd | sort | uniq -c
      5 /bin/bash
     23 /bin/false
      1 /bin/sh
      1 /bin/sync
     17 /usr/sbin/nologin

That's the output we saw at the top of the answer, of course. Let's sort it again to see the entries in order:

~$ awk -F:  '{print $7}' <  /etc/passwd | sort | uniq -c | sort
     17 /usr/sbin/nologin
      1 /bin/sh
      1 /bin/sync
     23 /bin/false
      5 /bin/bash

Wait, that can't be right, 17 comes before 1 and 5 after 23. The problem is that the items are being sorted lexicographically. Let's ask sort to sort them numerically, and in reverse order:

~$ awk -F:  '{print $7}' <  /etc/passwd | sort | uniq -c | sort -n -r
     23 /bin/false
     17 /usr/sbin/nologin
      5 /bin/bash
      1 /bin/sync
      1 /bin/sh

I think that explains everything in the original answer. If you're still unclear on the details of what those commands do, you can read the manual pages. If you're still unclear on the principles of what's going on, it might be better to start by reading a book (online or on paper) explaining Unix and Linux.

  • Can you explain a little more please? I'm not sure I follow what I'm reading. – Captain Man Mar 7 '16 at 18:27
  • is /bin/sync a shell? so any utility is a shell? – eloone Apr 21 '16 at 13:19
  • @eloone there are two (maybe three) meanings for "shell". One is, programs configured as a login shell in the password database. This is the meaning of the word intended by the phrase "non-interactive login shell". The other meaning is "a REPL suitable for day-to-day interactive work on a Unix system". Those are interactive. However, most of the programs usable in the REPL context also support non-interactive use as interpreters for some kind of scripting language (though "shell script" almost universally means "Bourne-family-shell script"). – James Youngman Apr 24 '16 at 9:34
  • 2
    You seem to be answering a different question. You're just listing the available shells and pointing out that some will never be interactive. I believe the OP is asking whether a non-interactive login shell is ever encountered in the wild. So whether shells that can be interactive, bash for example, ever launch non-interactive login shells. In other words, a "login shell" here, doesn't mean "a user's default login shell", it means a shell which has been launched as a login shell. See unix.stackexchange.com/a/46856/22222 – terdon Feb 3 '17 at 11:57
  • 1
    Or perhaps, given that the poster explicitly writes bash --login in the question, he's asking specifically about bash processes that do not have the i option set. (These arise from commands such as ssh somehost < script.sh; this will be a "login, non-interactive" shell in the senses specified in the INVOCATION section of the Bash manpage.) – Curt J. Sampson Oct 14 '17 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.