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

Re: syntactic triviality

On Dec 21, 10:33am, Ray Andrews wrote:
}      while true && true; do echo true; done
} I'm wondering if, strictly speaking, the 'do' needs to be necessary.  

Congratulations, you've just re-invented the SHORT_LOOPS variant syntax
you were ranting about doing away with yesterday.  :-)

Well, not exactly, but the general idea is the same.

The the syntax for "while" (and "if", etc.) actually allows any sort of
command separator within the conditions that follow the keyword.  So

    while true && true; echo true; do : ...; done

is also perfectly valid and would use the exit status of "echo" as the
loop condition.

} I'm guessing that it's necessary by fiat simply because it visually 
} pairs nicely with 'done'

Nope, rather it's to be distinct from a command separator.  You can even
use newlines:

    true && true
    echo true
    : ...

SHORT_LOOPS allows you to replace the do/done with a simpler expression in
cases where the syntax can be distinguished without it, e.g., consecutive

    while { true && true } { echo true }

or more likely

    while (( 1 )) { echo true }

But not:

    while (( 1 )); { echo true }

which is syntactically correct but incomplete (lacks a loop body, the
brace expression is still part of the condition).

So ... is that pointless?

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