Is it possible for there to be a login shell that's not interactive?
I've seen graphical login environments that do:
exec "$SHELL" -l -c 'exec start-window-or-session-manager'
or the equivalent of:
exec -a "-$SHELL" "$SHELL" <<EOF
exec start-window-or-session-manager
EOF
So that the session initialisation file (like ~/.profile
for Bourne-like shells (and the corresponding ones in /etc
for some)) be read and applied.
The first one doesn't work with all shells. -l
is supported by a great number of shells, but not all, and on some, like csh
/tcsh
, can't be used with -c
. The first character of argv[0]
being -
is understood by all shells though, as that's what login
uses to tell the shells they are login shells.
In the second case, the stdin of that shell is something other than a tty
device (<<
is implemented by a temporary regular file, or a pipe depending on the shell), so the shell is not interactive (the definition of interactive being when a human interacts with it).
Yes, non-interactive login shells are possible
$ head -1 /etc/profile
echo PROFILE BEING READ
$ echo echo hello | su -
PROFILE BEING READ
stdin: is not a tty
hello
$
A non-interactive login shell is unusual, but possible. If you start the shell with the zeroth argument (which is normally the name of the executable) set to a string beginning with a -
, it's a login shell, whether it's interactive or not.
$ ln -s /bin/bash ./-bash
$ echo 'shopt -p login_shell; echo $-' | HOME=/none PATH=.:$PATH -bash
shopt -s login_shell
hB
Your attempt bash -c date -bash
didn't work because that doesn't tell the shell to be a login shell: the zeroth argument is bash
, not -bash
. After bash has started, it sets the variable $0
to -bash
instead of the zeroth argument, but the zeroth argument is what matters.
You can run a noninteractive login shell with su -l
or su -
, but you need to arrange for standard input not to be a terminal while still being able to be authorized (without having to type a password, or arranging for your password to be at the start of the input). It may be easier with sudo: run sudo true
to get a presence credential, then while the credential is still valid run echo 'shopt -p login_shell; echo $-' | sudo -i
.
See also Difference between Login Shell and Non-Login Shell?