Differentiate Interactive login and non-interactive non-login shell

The only real misconception you seem to have is about what constitutes a non-interactive, login shell.

Briefly (see here for more details), with examples:

  • interactive login shell: You log into a remote computer via, for example ssh. Alternatively, you drop to a tty on your local machine (Ctrl+Alt+F1) and log in there.

  • interactive non-login shell: Open a new terminal.

  • non-interactive non-login shell: Run a script. All scripts run in their own subshell and this shell is not interactive. It only opens to execute the script and closes immediately once the script is finished.

  • non-interactive login shell: This is extremely rare, and you're unlikey to encounter it. One way of launching one is echo command | ssh server. When ssh is launched without a command (so ssh instead of ssh command which will run command on the remote shell) it starts a login shell. If the stdin of the ssh is not a tty, it starts a non-interactive shell. This is why echo command | ssh server will launch a non-interactive login shell. You can also start one with bash -l -c command.

If you want to play around with this, you can test for the various types of shell as follows:

  • Is this shell interactive?

    Check the contents of the $- variable. For interactive shells, it will include i:

    ## Normal shell, just running a command in a terminal: interacive
    $ echo $-
    himBHs
    ## Non interactive shell
    $ bash -c 'echo $-'
    hBc
    
  • Is this a login shell?

    There is no portable way of checking this but, for bash, you can check if the login_shell option is set:

    ## Normal shell, just running a command in a terminal: interacive
    $ shopt login_shell 
    login_shell     off
    ## Login shell; 
    $ ssh localhost
    $ shopt login_shell 
    login_shell     on
    

Putting all this together, here's one of each possible type of shell:

## Interactive, non-login shell. Regular terminal
$ echo $-; shopt login_shell
himBHs
login_shell     off

## Interactive login shell
$ bash -l
$ echo $-; shopt login_shell
himBHs
login_shell     on

## Non-interactive, non-login shell
$ bash -c 'echo $-; shopt login_shell'
hBc
login_shell     off

## 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

Essentially, whether a shell is login or not, interactive or not matters for exactly one reason:

The initialisation files and default options set depend on whether a shell is login or not and interactive or not.

Correspondingly, whether a shell is login or not or interactive or not depends solely on the invocation used - the exact command name and options.

The two properties are otherwise orthogonal - whether a shell is login or not has no bearing on determining whether it's interactive or not.

Bash starts a login shell if any of these are true:

  • argv[0], the name of the command it was invoked as, starts with a -
  • the -l option is specified

Similar, bash starts an interactive shell if any of these are true:

  • it was not specified a file to execute (i.e., the command wasn't bash some/file) or a command string to run (bash -c 'foo') (the actual condition is a bit more complex, see the manual)
  • the -i option was specified

Notably (and paradoxically), the latter implies that bash -ic 'foo' starts an interactive shell.

So the following starts a login, interactive shell, even though it has nothing whatsoever interactive about it and the invocation had nothing to do with logging in:

bash -lic true

That logging in via console or GUI starts a login shell (or maybe not) is entirely an effect of the login process using the appropriate invocation.

The conditions and effects are described in detail in the bash manual, section on Startup Files.


A major source of confusion is that there is another common meaning for "login" shell:

A user's login shell is the shell defined in that user's passwd entry (which may come from /etc/passwd, LDAP or some other source).

The login program, SSH, etc. start this shell as a login shell in the sense meant in the rest of the answer - with a leading - in the command name, usually. If you wanted to be particularly confusing, you could say:

Some login processes start the login shell of the user as a login shell.

Note that GUI login starts a login shell purely because the developers thought it convenient - LightDM runs a script on login which obviously isn't interactive and certainly doesn't depend on the user's login shell (in the second sense). Do not depend on the display manager starting a login shell, though - not all of them do, and on Wayland and GNOME, the login process doesn't use shell scripts at all.


Login shell:

The first process that executes under our user ID when we log in to a session. The login process tells the shell to behave as a login shell with a convention: passing argument 0, which is normally the name of the shell executable, with a “ - ” character prepended

Interactive shell:

Reads commands from user input on a tty. Among other things, such a shell reads startup files on activation, displays a prompt, and enables job control by default. The user can interact with the shell. A shell running a script is always a non-interactive shell.

Simply put: Interactive shell require user input, while non-interactive shell are run by scripts and don't require user inputs.

Tags:

Bash

Login