* How to detect that Zsh startup is result of exec zsh? @ 2016-09-13 12:23 ` Sebastian Gniazdowski 2016-09-13 13:06 ` Peter Stephenson 0 siblings, 1 reply; 7+ messages in thread From: Sebastian Gniazdowski @ 2016-09-13 12:23 UTC (permalink / raw) To: Zsh Users 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? Best regards, Sebastian Gniazdowski ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to detect that Zsh startup is result of exec zsh? 2016-09-13 12:23 ` How to detect that Zsh startup is result of exec zsh? Sebastian Gniazdowski @ 2016-09-13 13:06 ` Peter Stephenson 2016-09-13 13:19 ` Sebastian Gniazdowski 0 siblings, 1 reply; 7+ messages in thread From: Peter Stephenson @ 2016-09-13 13:06 UTC (permalink / raw) To: Zsh Users On Tue, 13 Sep 2016 14:23:23 +0200 Sebastian Gniazdowski <sgniazdowski@gmail.com> 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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to detect that Zsh startup is result of exec zsh? 2016-09-13 13:06 ` Peter Stephenson @ 2016-09-13 13:19 ` Sebastian Gniazdowski 2016-09-13 13:30 ` Peter Stephenson 0 siblings, 1 reply; 7+ messages in thread From: Sebastian Gniazdowski @ 2016-09-13 13:19 UTC (permalink / raw) To: Peter Stephenson; +Cc: Zsh Users On 13 September 2016 at 15:06, Peter Stephenson <p.stephenson@samsung.com> wrote: > On Tue, 13 Sep 2016 14:23:23 +0200 > Sebastian Gniazdowski <sgniazdowski@gmail.com> 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? > > 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. 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 Best regards, Sebastian Gniazdowski ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to detect that Zsh startup is result of exec zsh? 2016-09-13 13:19 ` Sebastian Gniazdowski @ 2016-09-13 13:30 ` Peter Stephenson 2016-09-15 6:31 ` Sebastian Gniazdowski 0 siblings, 1 reply; 7+ messages in thread From: Peter Stephenson @ 2016-09-13 13:30 UTC (permalink / raw) To: Zsh Users On Tue, 13 Sep 2016 15:19:03 +0200 Sebastian Gniazdowski <sgniazdowski@gmail.com> 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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to detect that Zsh startup is result of exec zsh? 2016-09-13 13:30 ` Peter Stephenson @ 2016-09-15 6:31 ` Sebastian Gniazdowski 2016-09-15 17:54 ` Lawrence Velázquez 0 siblings, 1 reply; 7+ messages in thread From: Sebastian Gniazdowski @ 2016-09-15 6:31 UTC (permalink / raw) To: Peter Stephenson; +Cc: Zsh Users On 13 September 2016 at 15:30, Peter Stephenson <p.stephenson@samsung.com> wrote: > 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 For me the two values are always the same: https://asciinema.org/a/1tpk0k6jii36iwrzawlr2ador This reminds me $ZSH_SUBSHELL that Mikael proposed, which is always 0 for me: https://asciinema.org/a/1jtfup4jrl14w7bps84wupqai Testing this on IRC bot gave correct result, i.e. "1" was assigned after running zsh IIRC. So maybe this is OS X issue, maybe even the same in both cases? Best regards, Sebastian Gniazdowski ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to detect that Zsh startup is result of exec zsh? 2016-09-15 6:31 ` Sebastian Gniazdowski @ 2016-09-15 17:54 ` Lawrence Velázquez 2016-09-15 18:02 ` Sebastian Gniazdowski 0 siblings, 1 reply; 7+ messages in thread From: Lawrence Velázquez @ 2016-09-15 17:54 UTC (permalink / raw) To: Sebastian Gniazdowski; +Cc: zsh-users > On Sep 15, 2016, at 2:31 AM, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote: > > On 13 September 2016 at 15:30, Peter Stephenson > <p.stephenson@samsung.com> wrote: >> 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 > > For me the two values are always the same: > https://asciinema.org/a/1tpk0k6jii36iwrzawlr2ador > > This reminds me $ZSH_SUBSHELL that Mikael proposed, which is always 0 for me: > https://asciinema.org/a/1jtfup4jrl14w7bps84wupqai > > Testing this on IRC bot gave correct result, i.e. "1" was assigned > after running zsh IIRC. So maybe this is OS X issue, maybe even the > same in both cases? You're not actually using any subshells. % zsh --version zsh 5.2 (x86_64-apple-darwin15.6.0) % typeset ZSH_SUBSHELL ZSH_SUBSHELL=0 % (typeset ZSH_SUBSHELL) ZSH_SUBSHELL=1 % ( (typeset ZSH_SUBSHELL) ) ZSH_SUBSHELL=2 % zmodload zsh/system % (( $$ == $sysparams[pid] )); echo $? 0 % ( (( $$ == $sysparams[pid] )); echo $?) 1 vq ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How to detect that Zsh startup is result of exec zsh? 2016-09-15 17:54 ` Lawrence Velázquez @ 2016-09-15 18:02 ` Sebastian Gniazdowski 0 siblings, 0 replies; 7+ messages in thread From: Sebastian Gniazdowski @ 2016-09-15 18:02 UTC (permalink / raw) To: Lawrence Velázquez; +Cc: Zsh Users On 15 September 2016 at 19:54, Lawrence Velázquez <vq@larryv.me> wrote: > You're not actually using any subshells. > > % zsh --version > zsh 5.2 (x86_64-apple-darwin15.6.0) > % typeset ZSH_SUBSHELL > ZSH_SUBSHELL=0 > % (typeset ZSH_SUBSHELL) > ZSH_SUBSHELL=1 > % ( (typeset ZSH_SUBSHELL) ) > ZSH_SUBSHELL=2 > % zmodload zsh/system > % (( $$ == $sysparams[pid] )); echo $? > 0 > % ( (( $$ == $sysparams[pid] )); echo $?) > 1 OK, so this works, but cannot be used to differentiate % exec zsh from % zsh, however now as I thought about it, it's easy: % export MYSHLVL=$SHLVL % zsh % echo $SHLVL $MYSHLVL 3 2 % exit % exec zsh % echo $SHLVL $MYSHLVL 2 2 Best regards, Sebastian Gniazdowski ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2016-09-15 19:45 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [not found] <CGME20160913122504eucas1p121867b112b7f08e125a324230d8296df@eucas1p1.samsung.com> 2016-09-13 12:23 ` How to detect that Zsh startup is result of exec zsh? Sebastian Gniazdowski 2016-09-13 13:06 ` Peter Stephenson 2016-09-13 13:19 ` Sebastian Gniazdowski 2016-09-13 13:30 ` Peter Stephenson 2016-09-15 6:31 ` Sebastian Gniazdowski 2016-09-15 17:54 ` Lawrence Velázquez 2016-09-15 18:02 ` Sebastian Gniazdowski
Code repositories for project(s) associated with this public inbox https://git.vuxu.org/mirror/zsh/ This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).