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?
2 Answers
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.
- 1Excellent answer! + 1 more confusing thing. Depends also on a compile-time define. Short docs for this option say: > If
NON_INTERACTIVE_LOGIN_SHELLSis defined, all login shells read the startup files, even if they are not interactive. C comment hereTrinitronX– TrinitronX2024-08-15 05:52:40 +00:00Commented Aug 15, 2024 at 5:52
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– Captain Man2016-03-07 18:27:01 +00:00Commented Mar 7, 2016 at 18:27
- is
/bin/synca shell? so any utility is a shell?eloone– eloone2016-04-21 13:19:01 +00:00Commented Apr 21, 2016 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– James Youngman2016-04-24 09:34:33 +00:00Commented Apr 24, 2016 at 9:34
- 2You 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/222222017-02-03 11:57:34 +00:00Commented Feb 3, 2017 at 11:57
- 1Or perhaps, given that the poster explicitly writes
bash --loginin the question, he's asking specifically aboutbashprocesses that do not have theioption set. (These arise from commands such asssh somehost < script.sh; this will be a "login, non-interactive" shell in the senses specified in the INVOCATION section of the Bash manpage.)cjs– cjs2017-10-14 05:59:31 +00:00Commented Oct 14, 2017 at 5:59
telnet towel.blinkenlights.nlfor a login shell that is non-interactive ;-)