bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why { is parsed differently depending on the context?


From: Eric Blake
Subject: Re: Why { is parsed differently depending on the context?
Date: Thu, 14 Feb 2019 16:38:47 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]