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

Re: 'while do done' hangs interactive zsh

2021-05-16 12:16:24 +0300, Arseny Maslennikov:
> Hi everyone!
> When zsh tries to execute "while do; done", it enters a busy loop:
> % dash -c 'while do; done'
> dash: 1: Syntax error: "do" unexpected
> % bash -c 'while do; done'
> bash: -c: line 1: syntax error near unexpected token `do'
> bash: -c: line 1: `while do; done'
> % zsh -c 'while do; done' 	# never finishes/prompts by itself
> ^C
> zsh -c 'while do; done'  27,73s user 0,00s system 99% cpu 27,732 total 4k maxrss
> Even more, if the user enters "while do; done" in an interactive zsh
> instance, the busy loop is not interruptible by ^C, ^\ or ^Z; the shell
> has to be killed via some external means.
> To be fair, I have discovered the bug with the semicolon omitted ("while do done").

As far as I can tell, it works as documented.

while LIST do LIST done

LIST is not explicitely specified, but it does appear to be a
list of 0 or more pipelines. Or in other words any script.


while do done

Runs the empty list of pipelines as long as the empty list of
pipelines is successful.

You'll see you can also do

if then echo the empty list is successful; fi

repeat 1000000 do done

myfunc() { }

if [ -e x ]; then else echo does not exist; fi

That does not even make zsh non-POSIX compliant, as those are
not valid POSIX sh syntax, so implementations are free to do
whatever they want.

If you want your *script* to be portable, you should not write:

while do echo yes; done

as a forever loop, as other shells require at least one pipeline
in the condition LIST (which to me just looks like an
arbitrary, unecessary limitation), but that does not mean shell
implementations have to return an error here.


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