Sometimes a Stackoverflow answer is so good that it helps me understand something I had kind of given up on. The distinctions of login and interactive shells are a good example. Some ressources had pointed me to the INVOCATION section of the bash man page. Here’s the explanation of what login and interactive shells are:
A login shell is one whose first character of argument zero is a –, or one started with the –login option.https://linux.die.net/man/1/bash
An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
I’m sure it’s correct and proper. From the perspective of someone trying to understand when
.bashrc is invoked, it’s also absurdly unhelpful. Askubuntu user terdon to the rescue.
They just provide examples of each of the four combinations (and two binary tests that will tell you if the shell you’re in is login/interactive or nor) but from those examples, the defining characteristics become so much clearer. A shell is a login shell if I changed user when entering it be that by using
ssh or logging in on a
tty. An interactive shell is basically anything with a prompt and a non-interactive one is running something scripted. The combinations are then as follows:
|Examples||Interactive shell||Non-interactive shell|
|Login shell||TTY, su -, ssh||Piping commands into ssh|
|Non-login shell||A terminal emulator, starting a shell within a login shell||Scripts|
I’m not summarizing to replace the answer, just to check that I understand. terdon’s examples do a much better job of explaining it – go have a read.