login shell check is not working in .profile
shopt
is not a valid command in the POSIX sh
shell (nor zsh
, which uses setopt
/unsetopt
), so will error out - making your test return non-zero unconditionally:
$ bash -lc 'shopt -q login_shell; echo $?'
0
but
$ sh -lc 'shopt -q login_shell; echo $?'
sh: 1: shopt: not found
127
and
$ zsh -lc 'shopt -q login_shell; echo $?'
zsh:1: command not found: shopt
127
Since ~/.profile
(as well as /etc/profile
, plus the files in /etc/profile.d
that it sources) may be read by other shells, best practice is to keep it POSIX complient. AFAIK the POSIX way to check for a login shell is to test whether $0
begins with a -
character ex.
case $0 in
-*) echo "login shell"
;;
*) echo "non-login shell"
;;
esac