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

"var=value exec > file" ignores the assignment?



If 'exec' has no command arg but has both parameter assignment
and redirection, the assignment seems to be ignored.

The following script outputs "x = 0" into test.log:

#!/usr/local/bin/zsh
setopt POSIX_BUILTINS 
x=0
x=1 exec > test.log
echo "x = $x"

'ksh' and 'bash -posix' (or /bin/sh) output "x = 1", which I
guess what POSIX requires.

Moreover, assuming there is no command named 'junk',

x=$(junk) exec > test.log

does NOT issue "command not found: junk" error.

The following patch *seems* to fix these, but I believe this
is at most a partial fix (or regression at the worst).

Either with or without the patch, and either POSIX_BUILTINS is
set or unset, the following outputs "x = 1" (/bin/sh outputs
"x = 0"). But I don't know whether this need be fixed (or how
to fix it if it need be).

x=0
true | x=1 exec | true
echo "x = $x"


diff --git a/Src/exec.c b/Src/exec.c
index 1a6149a..fdb2470 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3305,6 +3305,13 @@ execcmd(Estate state, int input, int output, int how, int last1)
 	    closemn(mfds, i, REDIR_CLOSE);
 
     if (nullexec) {
+	/*
+	 * If nullexec is 2, we have variables to add with the redirections
+	 * in place.
+	 */
+	if (varspc)
+	    addvars(state, varspc, 0);
+	lastval = errflag ? errflag : cmdoutval;
 	if (nullexec == 1) {
 	    /*
 	     * If nullexec is 1 we specifically *don't* restore the original
@@ -3315,13 +3322,6 @@ execcmd(Estate state, int input, int output, int how, int last1)
 		    zclose(save[i]);
 	    goto done;
 	}
-	/*
-	 * If nullexec is 2, we have variables to add with the redirections
-	 * in place.
-	 */
-	if (varspc)
-	    addvars(state, varspc, 0);
-	lastval = errflag ? errflag : cmdoutval;
 	if (isset(XTRACE)) {
 	    fputc('\n', xtrerr);
 	    fflush(xtrerr);




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