Is Rule 7 of POSIX shell grammar rules written correctly?

The POSIX shell standard grammar rules are at

Shell Command Language

I am trying to understand Rule 7 and I don't. I think there may be some mistakes there. I am not complaining about the standard; rather, I am concerned that my perception is wrong, and I don't understand something important.

Here are my questions. Please tell me for each question, if the standard is simply written incorrectly, or what is it that I don't understand.

  1. Rule 7 is only referenced with yacc symbols cmd_name and cmd_word, not with any symbols that have to do with assignments, yet it is labeled "Assignment preceding command name". Is that a misleading label ?

  2. Rule 7a for the first word, in some cases refers to Rule 7b, which is labeled "not the first word". Is that a misleading label ?

  3. The yacc symbol cmd_word refers to rule 7b, which says, if the word contains '=' after its first character, then it is an assignment - and not anything to do with a command. Should rule 7b simply state that it is not allowed to have '=' after the first character?

  4. Rule 7a is only referenced during parsing simple commands, which cannot have any reserved words in them. Yet Rule 7a in some cases refers to Rule 1, which differentiates between a reserved word and ordinary word.

If there is no possibility of having a reserved word in Rule 7a, there would be no point to refer to any additional Rule 1 at all. Then, there would be no point in having a separate Rule 7a at all. That is because, if the word did not contain '=', Rule 7b says nothing, so we have WORD, and if it did contain '=', then 7b applies. Thus we would only need 7b. Then also, there would be no need for cmd_name symbol, we could just have cmd_word.

Is Rule 7a and cmd_name symbol needed?

No. Consider the following: you have an executable called "foo=bar", which is a valid name for a file. What would these lines do:

# cat foo=bar
echo "hello world"

# ./foo=bar
??

# typeset foo=bar
??

# x=$(foo=bar)
??

# foo=bar
?? 

Would it assign the value "bar" to a variable "foo" or would it execute the command foo=bar ?

And all this deals with similar situations. That may seem pretty picky, but when designing a language you cannot assume that one only uses things that make sense at first glance.

I hope this helps.

bakunin

1 Like

In addition to what bakunin has already said, rule 7 applies in cases like:

  1. IFS=, for i in abc,def,chi,jul;do echo "$i";done which generates a syntax error because for is not recognized as a keyword because it is not the 1st word in the command,
  2. IFS=, PS2='Enter continuation line: ' read var1 var2 var3 forces the read command to be invoked with values for the environment variables IFS and PS2 that apply only during the execution of the read command (without changing the values of those variables in the current shell execution environment), and
  3. =abc read something tries to run a command named =abc with the operands read and something rather than attempting to set the variable with no name to the string abc in the environment of the command read invoked with the operand something .

Thank you bakunin for your reply, but your reply is not relevant to my question. I spent a lot of time writing the question and I guess you did not have time to read it as carefully as I had wrote it.

However, your reply did help, in that, while thinking whether it is relevant or not, I finally understood the whole thing.

For the benefit of newbies, I will post my understanding and answers here:

1 and 2. Yes. Of course these labels are misleading, and make the grammar hard to understand for newbies.

  1. No. Instead, it is best to apply Rule 7b also to the 2 productions in the grammar, that reduce cmd_prefix to ASSIGNMENT_WORD.

  2. No. I misunderstood here how the "rules" are applied. I thought, you first perform the reduction, then somehow apply the rule (which makes no sense). Instead, you use the rule first, in order to allow the reduction.