On 2/14/19 11:05 AM, Peng Yu wrote: > Hi, > > `echo {` treats `{` as WORD. > > `{ echo; }` treats `{` as a token of `{`. > > `{a` treats `{a` as a WORD. > > I don't see the point why yylex() treat `{` context dependently. > Wouldn't it better just treat a bare `{` as a token of `{`? > > What is the reasoning behind the current design of the syntax?
Historical practice. And it is for the same reason that 'if' is treated as IF when it appears in certain spots (as in 'if :; then ...', but as a WORD in others (as in 'echo if'). Basically, '{' is a reserved word, and NOT an operator, so it does NOT get the same treatment as '(', '<', and so on. Changing it now would break scripts that have relied on the fact that '{' is parsed as a reserved word when in the correct syntactical locations (and therefore '{}' does not need quoting because it is not spelled the same as a reserved word) rather than an operator (where constructs like 'find . -exec {} \;' would be broken). http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_10 may be a helpful read for rules on deciding whether a TOKEN is further resolved into reserved word or a plain WORD (although remember that bash's parser recognizes more things than are required by the POSIX grammar) -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
signature.asc
Description: OpenPGP digital signature