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

Re: dangerous behavior of while in zsh 5.6 ?



On Wed, Oct 3, 2018 at 7:47 AM Marc Chantreux <eiro@xxxxxxxxx> wrote:
>
> a end of line after a condition is interpreted as an empty
> true block so:
>
>     while (( i-- ))
>         print $i

This is just a syntax error for me (or prints the PS2 prompt if
interactive), so I'm not sure how you're getting it to do anything.

This would certainly not be something new or different in 5.6.2.

In shell syntax, a newline is the same as a semicolon, so what you've
written should be the same as:

  while (( i-- )); print $i

To make that a complete loop statement you have to follow it with "do
... done" or something in braces or parens.

> should   mean:
>
>     while (( i-- )) {print $i}

No, it should mean

  while { (( i-- )); print $i; }

which will be an infiinte loop (if you ever supply the "do" block,
otherwise it's just a syntax error) because the exit status of the
loop condition { ... } will be that of the print statement, which is
never false.

> so it doesn't behave like the other loops:
>
>     repeat 5 print $[i++]
>     while (( i-- )) print $i

It's actually "repeat" that doesn't behave the same here, because
"repeat" has no test condition to follow the number of repetitions.
Thus

  repeat 5 ; print $[i++]

is

  repeat 5 { ; print $[i++]; }

Conversely the test condition for "while" is defined to be the whole
sequence of commands up to the keyword "do" or (with shortloops) one
of "{" or "(" or "((".  Commands in that sequence may be separated &&
or || or | or semicolon or newline.  This is not a bug, it's the way
the syntax is designed to work.



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