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 15:19:03 +0200
Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> I'm assigning unique ID to every Zsh. If one runs Zsh in side Zsh,
> either by exec or by new Zsh, the ID is inherited. However, there's
> Tmux. There, ID is not inherited in both cases – person opening new
> pane gets new Zsh, with new ID. But the exec case could still inherit
> ID – replacing Zsh inside Tmux is fine to inherit ID. That's why I
> need to differentiate the two

But that's not a difference between the two case in a subshell.  In both

(exec zsh)
(zsh)

the parent shell still exists.  The exec in a subshell doesn't replace
the parent shell, unlike exec in the parent shell itself.

So what you actually need to distinguish is the existence of the
subshell.

You can do that by storng the PID of each process and associating it
with the ID.  If the PID is the same as that of the parent shell (which
is what happens with exec in the parent shell) the ID can stay the same;
in any other case it will be different.  This works generally,
regardless of any fork / exec behaviour, with the usual proviso that
once the PID has exited the number my be reused.

It sounds like you might be in danger of reniventing UNIX process
management...

BTW, telling you're in a subshell when you're still in the parent shell
is actually easy:

zmodload zsh/system
if (( $$ ==  $sysparams[pid] )); then
  print "I'm in the parent shell"
else
  print "I'm in a subshell"
fi

However, that's too early for you --- you need to know at the start of
the new zsh.  I think some external mapping to PIDs is the only reliable
way.

pws



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