The accepted answer for a similar question for bash does not seem to work for zsh.  In fact, if I copy basically the same code given in that answer, to produce the script#!/usr/bin/zsh -# test.sh[[ $_ != $0 ]] && echo sourced\n || echo subshell\nthe output hardly ever corresponds to the actual situation:zsh% chmod +x ./test.shzsh% env -i /usr/bin/zsh -fzsh% ./test.shsourcedzsh% /usr/bin/zsh ./test.shsourcedzsh% /bin/bash ./test.shsourcedzsh% source ./test.shsubshellzsh% . ./test.shsubshellzsh% env -i /bin/bash --norc --noprofilebash-3.2$ ./test.shsourcedbash-3.2$ /usr/bin/zsh ./test.shsourcedbash-3.2$ /bin/bash ./test.shsourcedbash-3.2$ source ./test.shsourcedbash-3.2$ . ./test.shsourcedWhen the current interactive shell is zsh, the script gets it exactly wrong every time.  It fares a bit better under bash (though in a way reminiscent of the stopped watch that gets the time exactly right twice a day).These truly abysmal results give me little confidence in this approach.Is there something better?
How can a zsh script test whether it is being sourced?
zsh