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

Re: $PPID not updated when the PPID changes (parent killed)



2021-05-17 16:15:24 -0700, Bart Schaefer:
[...]
> Indeed.  I'm tempted to say
> 
>   Just as with $$, the value is updated only within a new shell
>   that increases $SHLVL, and not in other subshells.
[...]

$ (SHLVL=0; zsh -c 'echo $SHLVL $$ $PPID; sleep 2; exec zsh -c "echo \$SHLVL \$$ \$PPID"' & sleep 1)
1 485756 485755
1 485756 302910

(where in that case 302910 is the pid of the systemd user's
session child subreaper, traditionally, one would get 1
instead).

$PPID, like $$ are set when the shell is initialised, just after
it had been executed.

In the example above, the same 485756 process executed zsh twice
and $$ and $PPID were set twice.

Because of the exec (and it would have been the same without
exec in a few contexts where zsh skips forks implicitly), $SHLVL
is not incremented (well it is decremented by the first zsh
before exec and incremented again by the second).

$SHLVL (a (t)csh thing IIRC, not handled by all shells) is the
number of shell *invocations* you need to exit before that logs
you out.

I would find it much clearer if we described $$/$PPID in
relation to shell invocation initialisation like POSIX does.

-- 
Stephane





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