On 7/29/19 1:08 PM, Stephane Chazelas wrote:
> That's a follow-up on
> https://www.mail-archive.com/bug-bash@gnu.org/msg23451.html
> 
> Is there anything in the POSIX spec that allows:
> 
> x[ foo
> 
> To be interpreted as anything other than invoking the "x["
> command with "foo" as argument?
> 
> I had the vague recollection that there was but I can't find it
> now. If there's not, that would be a bug in the spec as several
> shells including ksh, bash, zsh, yash treat it as the start of
> an array element assignment (like in:
> 
> x[ foo
> + 1]=value

Ouch. I think you've identified a real problem.

In XSH 2.13.3, we explicitly added wording to allow unmatched unquoted
'[' in a word to be used in its role similar to 'test' (the difference
being whether a later ']' argument is necessary):

If the pattern contains an open bracket ( '[' ) that does not introduce
a bracket expression as in XBD RE Bracket Expression, ...
If the pattern does not match any existing filenames or pathnames, the
pattern string shall be left unchanged.

So by that argument, if the shell parses 'x[' as a word, then because it
does not form a valid glob, it must be used unchanged as the command
name.  But that explicit wording does not cover whether 'x[' has to be
delimited as a word.

XSH 2.3 states in rule 7 that an unquoted blank ends the delimiting of
any prior word, but the behavior you are showing for shells that parse
a[b]= as an array assignment are trying to find the matching ] before
delimiting the first word (so the shell extension of array assignment is
somehow acting as a quoting context that prevents the whitespace thus
parsed from being the unquoted blank that ends the delimiting of the word).

The shell grammar, at XSH 2.10.1, allows for array assignments in rule 7b:

If the TOKEN contains an unquoted (as determined while applying rule 4
from Token Recognition) <equals-sign> character that is not part of an
embedded parameter expansion, command substitution, or arithmetic
expansion construct (as determined while applying rule 5 from Token
Recognition):

    If the TOKEN begins with '=', then rule 1 shall be applied.

    If all the characters in the TOKEN preceding the first such
<equals-sign> form a valid name (see XBD Name), the token
ASSIGNMENT_WORD shall be returned.

    Otherwise, it is unspecified whether rule 1 is applied or
ASSIGNMENT_WORD is returned.

with the intent that a[b]= can be an ASSIGNMENT_WORD in shells with
array extensions, but can also be WORD for shells that treat it as glob
to determine a command name.  But without any explicit specification of
permitting whitespace in the array arguments, it looks like there is a
discrepancy between POSIX requirements and existing shell behavior.


-- 
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