Score:0

login shell check is not working in .profile

vn flag

Ubuntu 20.04, 5.8.0-63-generic, gnome-shell, gdm3

My login shell is set to /bin/sh

and I have this line in my $HOME/.profile file:

shopt -q login_shell > /dev/null 2>&1 || export SHELL=/bin/zsh

Now from my understanding export SHELL=/bin/zsh should not be executed on login right? because it is a login shell so shopt -q login_shell > /dev/null 2>&1 is True.

But I am getting $SHELL as /bin/zsh after login.

Actually setting $SHELL is not my concern, I want to use shopt -q login_shell > /dev/null 2>&1 in .zshrc logic. I need to get that working to selectively run things in non-login shell.

Can't figure out what's going wrong. Need Help!

Edit:

Shopt is not a /bin/sh command. But even changing login shell to /bin/bash doesn't help.

Score:1
hr flag

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
Sayan Dey avatar
vn flag
Right Thanks. But even changing to /bin/bash not working. What to do if I want to run something just during login. Edited the question.
hr flag
@SayanDey I don't think `shopt` is legal in `zsh` either - it's `setopt`/`unsetopt`. IIRC the *portable* way to test for a login shell is to check if the first character of `$0` is a `-`. See edit.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.