参见英文答案 >
Is a semicolon prohibited after NAME in `for NAME do …`?1个
for i do echo $i; done
这怎么合法? (我希望它能为我写一个额外的分号;做回声$i;完成)它适用于bash,dash,zsh和ksh.标准(我的意思是http://pubs.opengroup.org/onlinepubs/9699919799/)规定:
The for loop requires that the reserved words do and done be used to delimit the sequence of commands. The format for the for loop is as follows: for name [ in [word ... ]] do compound-list done
很明显,当省略“in word”时,do就是一个分隔符.因此暗示似乎是[in [word ..]]之后的分隔符(换行符)实际上属于右侧右括号内.有人能指出标准中的任何理由证明这种(IMO)可怕的滥用语言吗?
如果查看
GNU
man
page,您会看到循环具有以下语法:
for The Syntax of the for command is: for name [ [in [words …] ] ; ] do commands; done
因此,您可以看到额外的分号是可选部分的一部分.
linux-die man page也是如此.