This is a slightly esoteric Zsh question, of which there may not be a concrete answer.
I am trying to detect a particular context of a Zsh when instantiated in a particular way. How to do this is not immediately obvious given the environment variables commonly used for this purpose (such as
$-), at least as far as I am aware.
Here is a Zsh instantiated in a normally detectable way:
export HELLO='world' zsh -c 'echo "$SHLVL $ZSH_SUBSHELL $ZSH_EVAL_CONTEXT $- $HELLO"' # 2 0 cmdarg 569X world
As opposed to a Zsh instantiated in a way that obscures detection:
export HELLO='world' (zsh -c 'echo "$SHLVL $ZSH_SUBSHELL $ZSH_EVAL_CONTEXT $- $HELLO"') # 1 0 cmdarg 569X world
The Zsh called within the subshell has access to the parent environment's variables, as is evident by the presence of
$SHLVL is tricked into reporting
1 and unfortunately
$ZSH_SUBSHELL, with a value of
0, is of no help in this case.
How can I detect, using standard detection methods, the fact that the second case has access to a parent environment? I don't want to have to rely on checking variables set by my self in the parent context that dirty the environment solely for this purpose, if I don't have to.
Perhaps it would help me if someone could explain how Zsh deciphers the
$SHLVL and why placing that code in a subshell obscures that detection. My rough understanding is that the subshell forks the environment (anything that is exported), and that
$SHLVL is not exported.