Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command list does not exit immediately #45

Open
ajschorr opened this issue Dec 4, 2021 · 1 comment
Open

command list does not exit immediately #45

ajschorr opened this issue Dec 4, 2021 · 1 comment

Comments

@ajschorr
Copy link

ajschorr commented Dec 4, 2021

This looks like a bug to me. This is using tcsh 6.20.00:

bash-4.4$ csh -c 'false || exit; echo hello'
hello
bash-4.4$

Why didn't the shell exit prior to executing the 'echo hello' command?

It seems as if the exit has a delayed effect and happens only after the entire list has been executed.

In an interactive shell, one sees:

bash-4.4$ csh
schorr@ti129: false || exit; echo hello
hello
exit
bash-4.4$

@Krush206
Copy link

Krush206 commented Jun 7, 2024

I got used to this behavior, thus no longer saw it as a bug. I found it useful for one-line procedures.

( exit ( -e myfile ) && ( ( echo mytext > myfile ) >& /dev/null || echo Cannot create file. ) || echo File exists.

Anyhow, this behavior can be avoided with the following patch.

--- a/sh.sem.c
+++ b/sh.sem.c
@@ -658,13 +658,15 @@ execute(struct command *t, volatile int wanttty, int *pipein, int *pipeout,
 		if (setexit() == 0)
 		    func(t, bifunc);
 		resexit(oldexit);
-		haderr = ohaderr;
 
 		if (adrof(STRprintexitvalue)) {
 		    int rv = getstatus();
 		    if (rv != 0)
 			xprintf(CGETS(17, 2, "Exit %d\n"), rv);
 		}
+		if (bifunc->bfunct == doexit && !haderr)
+		    reset();
+		haderr = ohaderr;
 	    }
 	    break;
 	}

Should be noted this doesn't work for one-line ifs, and I don't think would make a problem, since exit inherits the same expressiveness of if.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants