0

From Bash Manual

Invoked as an interactive login shell, or with --login

When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive login shell exits, or a non-interactive login shell executes the exit builtin command, Bash reads and executes commands from the file ~/.bash_logout, if it exists.

Invoked as an interactive non-login shell

...

Invoked non-interactively

When Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

but the value of the PATH variable is not used to search for the filename.

As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.

Which case does a noninteractive login shell belong to, the first case, or the third case?

The first case "Invoked as an interactive login shell, or with --login" contains the scenario of "non-interactive shell with the --login option", so I deduce that

  • the first case is for login shells regardless of being interactive or noninteractive, and

  • the third case is for noninteractive nonlogin shells.

Am I correct?

Thanks.

2

No, you’re not correct. Bash behaves as documented:

  • the first section applies to interactive login shells, and to non-interactive shells started with the --login flag;
  • the third section applies to non-interactive shells, including non-interactive login shells not started with the --login flag.

A shell can be a login shell without the --login flag. If you look at /proc/$$/cmdline from a Bash shell started by SSH on a Linux system, you’ll see it was started as -bash — the leading hyphen is the usual way of starting a login shell, and isn’t covered by the first section if it ends up being non-interactive. However if one wanted a non-interactive login shell for whatever reason, one would typically use --login to get it.

  • Thanks. What kinds of "non-interactive shells" does the third section refer to? Are "non-interactive shells started with the --login flag" still noninteractive shells? Are "non-interactive shells started with the --login flag" login shells? – Tim Apr 4 '18 at 22:37
  • See the description of the non-interactive shell in your quote: “to run a shell script, for example”. A non-interactive shell is non-interactive, yes, and a shell started with --login is a login shell, yes. I gave you all the tools you need to verify this for yourself, I encourage you to use them... – Stephen Kitt Apr 4 '18 at 22:41
  • Thanks. So "non-interactive shells started with the --login flag" are noninteractive login shells. Does the third section apply to all non-interactive login shells except those which are "non-interactive shells started with the --login flag"? – Tim Apr 4 '18 at 22:42
  • This is getting seriously theoretical, there isn’t a practical, common way of starting a non-interactive login shell except by using the --login option. – Stephen Kitt Apr 4 '18 at 22:43
  • 1
    @JdeBP for some value of “practical”... – Stephen Kitt Apr 4 '18 at 23:13

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.