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

[PATCH] [bug] $SHLVL decremented for the last command of a subshell



2016-08-22 07:17:23 +0100, Stephane Chazelas:
> (from https://unix.stackexchange.com/questions/304917/detect-when-zsh-is-running-within-the-subshell-of-another-shell)
> 
> $ printenv SHLVL
> 1
> $ (printenv SHLVL)
> 0
> $ (printenv SHLVL;:)
> 1
> $ (:;printenv SHLVL)
> 0
> 
> 
> http://www.zsh.org/mla/workers/1999/msg02472.html
> sounds like a potential culprit.
[...]

I think the change below should be enough (also condensed the 2
"if"s below (unrelated)):

diff --git a/Src/exec.c b/Src/exec.c
index 9b24d38..54ececc 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3694,14 +3694,12 @@ execcmd(Estate state, int input, int output, int how, int last1)
 		restore_params(restorelist, removelist);
 
 	} else {
-	    if (!forked)
+	    if (!forked && !subsh)
 		setiparam("SHLVL", --shlvl);
-	    if (do_exec) {
+	    if (do_exec && !subsh && isset(RCS) && interact && !nohistsave)
 		/* If we are exec'ing a command, and we are not *
 		 * in a subshell, then save the history file.   */
-		if (!subsh && isset(RCS) && interact && !nohistsave)
-		    savehistfile(NULL, 1, HFILE_USE_OPTIONS);
-	    }
+	        savehistfile(NULL, 1, HFILE_USE_OPTIONS);
 	    if (type == WC_SIMPLE || type == WC_TYPESET) {
 		if (varspc) {
 		    int addflags = ADDVAR_EXPORT|ADDVAR_RESTRICT;



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