Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

do-list optional in Bourne shell while loop?

20 views
Skip to first unread message

Edward McGuire

unread,
Jan 10, 2023, 3:10:34 PM1/10/23
to
I was reading manual pages for Bourne shell, and was surprised to see the syntax of while-done documented as "while list [ do list ] done". The surprise is that the do-list is bracketed (optional).

The brackets appear in the Unix Version 7 manual page published by Sven Mascheck (https://www.in-ulm.de/~mascheck/bourne/v7/), and in the manual page released by Gunnar Ritter with the Heirloom Bourne shell (https://heirloom.sourceforge.net/sh/sh.1.html).

Is this just a long-standing documentation bug? The Heirloom shell actually throws a syntax error when its input is "while true ; done".

Janis Papanagnou

unread,
Jan 10, 2023, 10:58:18 PM1/10/23
to
Is it [in Unix Version 7] a bug? - I suppose we'd need some old
(original) Bourne shell to test its behavior. - If it would work
in that old shell it could be considered an implementation error
in the clone. Code like i=5 ; while echo $i ; let i=i-1 ; done
would work. Anyway. Being curious I had a peek into the C source
code and (as far as I understood the macro'ized C code correctly)
the while statement expects a 'do' and a 'done'. A comment at the
beginning lists as an "item": "for ... while ... do ... done" ,
a syntax that resembles the Algol 68 generalized loop; which may
(as far as I can tell) be defined formally like
[ for v ] [ from f ] [ by b ] [ to t ] [ while w ] do s od
where almost everything is optional but the 'do' part. Despite
the Algol like "item" in Bourne shell 'for' and 'while' seem to
be separate commands (as we also know from the shell successors).
Interestingly I noticed that the 'for' loop does not seem to need
'do' and 'done'; these can be replaced by braces! - And that is
obviously still supported by some of the modern shells (ksh, zsh,
bash, but not, e.g., by ash):

$ for i in a b ; { echo $i ; }
a
b

(though this seems undocumented). - Funny anyway, and new to me.

Janis

Edward McGuire

unread,
Jan 11, 2023, 5:27:49 PM1/11/23
to
On Tuesday, January 10, 2023 at 9:58:18 PM UTC-6, Janis Papanagnou wrote:
> the 'for' loop does not seem to need
> 'do' and 'done'; these can be replaced by braces! - And that is
> obviously still supported by some of the modern shells (ksh, zsh,
> bash, but not, e.g., by ash)

Great find, I had no idea.
0 new messages