Re: Loops and pipes
Philippe Altherr <[email protected]> Tue, 4 Aug 2026 00:37:25 +0200
| Newsgroups | gmane.comp.shells.zsh.devel |
|---|---|
| Message-ID | <CAGdYchsAhEE5jRd3o9Kc=KBL4qdCbGs4N3n4bHH1wDnoaQrhEQ@mail.gmail.com> |
--000000000000687b4106582c2f83
Content-Type: text/plain; charset="UTF-8"
>
> 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
--000000000000687b4106582c2f83
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_quote gmail_quote_container"><blockquo=
te class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px =
solid rgb(204,204,204);padding-left:1ex">Counter-example:<br>
<br>
for x in 1 2 3; do (echo IN $x; break; echo OUT $x); done<br>
IN 1<br>
IN 2<br>
IN 3<br>
<br>
It'd take some finagling to get break/continue to propagate up. Hijack<=
br>
a signal?=C2=A0 Reserve an exit status of the subshell?<br></blockquote><di=
v><br></div><div>I don't think that trying to make "break" an=
d "continue"=C2=A0work 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 fun=
ction and not just from the subshell. After all, one could use "exit&q=
uot; in order to just exit from the subshell. Or should then "exit&quo=
t; exit the whole script?</div><div><br></div><div>In the example above, I =
would rather signal an error or print a warning. One can use "return&q=
uot; or "exit" to achieve the same result as what "break&quo=
t; currently=C2=A0does.</div><div><br></div><div>ksh works like Zsh ("=
break" triggers no error and has the same effect as "return"=
) but bash signals an error:</div><div><br></div><div><font face=3D"monospa=
ce">% bash -c 'for x in 1 2 3; do (echo IN $x; break; echo OUT $x); don=
e'<br>IN 1<br>bash: line 1: break: only meaningful in a `for', `whi=
le', or `until' loop<br>OUT 1<br>IN 2<br>bash: line 1: break: only =
meaningful in a `for', `while', or `until' loop<br>OUT 2<br>IN =
3<br>bash: line 1: break: only meaningful in a `for', `while', or `=
until' loop<br>OUT 3</font><br></div><div><br></div><div>Philippe</div>=
<div><br></div></div></div>
--000000000000687b4106582c2f83--