Bash – Does a noninteractive login shell execute `~/.profile` or a file whose name is `$BASH_ENV`

bashconfigurationprofile

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.

Best Answer

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.

Related Question