21

The expresion:

$ ksh -c 'for ((i=0;i<10;i++)){ echo $i;}'
0
1
2
3
4

works correctly.

I fail to find the description of this syntax in which the {...;} is replacing the ; do ...; done in the manual.

Is there a reference in the manual? For which shells do that work correctly?

1 Answers1

29

{ ...; } was already accepted in place of do ...; done in for loops in the first version of the Bourne shell in Unix V7 in the late 70s (see the source code) though never documented as far as I know.

All of ksh (both the original one derived from the Bourne shell, and the ksh93 rewrite), pdksh¹ (and derivatives), bash (from the start in 1989) and zsh support it. I don't know of any ash derivative that does. yash doesn't either but yash was written to the POSIX standard.

In zsh, support was added with 2.0 in 1991, but as part of a new range of short forms for most constructs which goes beyond the Bourne syntax. It's not only for i in 1 2; { ...; }, it's for i in 1 2; any-command (and variants thereof). And it is documented there and has been since its introduction.

AFAIK, beside zsh, only pdksh documents it (for both for and select).

As for the ((init; condition; action)) form, that comes from ksh93 and was copied by zsh and bash.

Neither that ((...)) nor the { ...; } in place of do ...; done are POSIX.

See also What is the purpose of the "do" keyword in Bash for loops? for more for loop forms.


¹ not earlier versions, nor the Forsyth shell on which it is based. It was added in 5.1.1 in 1994 and that probably explains why it became documented there