Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How to detect that Zsh startup is result of exec zsh?



On Tue, 13 Sep 2016 14:23:23 +0200
Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> Hello,
> first `exec zsh` can be detected via SHLVL. It will be "1". And some
> other variable set in .zshrc earlier will be non-zero. But what with
> `exec zsh` ran from sub-shell?

So you're correct that these two are the same (both before and after
Stephane's recent fix):

% (exec zsh -c 'echo $SHLVL')
3
% (zsh -c 'echo $SHLVL')
3

(for some value of "3".)

But, in fact, those cases *are* handled identically:

In case 1:

- Fork for the subshell
- Exec the new zsh as requested, replacing the subshell.

In case 2:

- Fork for the subshell
- Get to the last command in the subshell.  No further fork is necessary
  because the subshell is exiting here.  So just "exec" the new zsh.

More widely, in fact, because fork and exec are so basic to UNIX-like
process handling it's often not possible, and usually not useful, to
tell this sort of thing apart.

So the real question, as so often, is what you are actually trying to
do?  What makes it necessary that you treat the cases differently?  If
you can address that there may be an answer.

pws



Messages sorted by: Reverse Date, Date, Thread, Author