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

Re: Loops and pipes



Counter-example:

for x in 1 2 3; do (echo IN $x; break; echo OUT $x); done
IN 1
IN 2
IN 3

It'd take some finagling to get break/continue to propagate up. Hijack
a signal?  Reserve an exit status of the subshell?

I don't think that trying to make "break" and "continue" work in such a context would be a good idea. If that worked then one could argue that the "return" in "() { (echo IN; return; echo OUT1); echo OUT2 }" should return from the function and not just from the subshell. After all, one could use "exit" in order to just exit from the subshell. Or should then "exit" exit the whole script?

In the example above, I would rather signal an error or print a warning. One can use "return" or "exit" to achieve the same result as what "break" currently does.

ksh works like Zsh ("break" triggers no error and has the same effect as "return") but bash signals an error:

% bash -c 'for x in 1 2 3; do (echo IN $x; break; echo OUT $x); done'
IN 1
bash: line 1: break: only meaningful in a `for', `while', or `until' loop
OUT 1
IN 2
bash: line 1: break: only meaningful in a `for', `while', or `until' loop
OUT 2
IN 3
bash: line 1: break: only meaningful in a `for', `while', or `until' loop
OUT 3


Philippe



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