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!


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


1 Answer 1


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 $?'



$ sh -lc 'shopt -q login_shell; echo $?'
sh: 1: shopt: not found


$ zsh -lc 'shopt -q login_shell; echo $?'
zsh:1: command not found: shopt

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"
  • 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.
    – Sayan Dey
    Jul 31, 2021 at 2:31
  • @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. Jul 31, 2021 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .