How does the POSIX shell grammar http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_10

accept a simple command with assignment such as:

FOO=bar foobar

The first word here must be accepted as token ASSIGNMENT_WORD by Rule 7b (although this rule is not specified explicitly to apply to the productions involving ASSIGNMENT_WORD , but I guess that is understood).

Then there is no way to accept the second word. The only way to try to reduce it would be with cmd_word production, but that requires Rule 7b to say this is a WORD and Rule 7b does not say anything about words that do not contain =.

One has to add to Rule 7b a statement such as:

If the TOKEN does not contain `=` then it is a `WORD`.  

Without this, the grammar as written currently is faulty. Am I correct?


The grammar will only assign one word; successive words after that will be treated as a command.

Keep in mind that the rule you are citing refers to a TOKEN, and that cannot contain an unquoted space. See 2.3 Token Recognition, e.g.,

  1. If the current character is an unquoted <blank>, any token containing the previous character is delimited and the current character shall be discarded.
  • yes I know this is how the shell will work. But that is not my question. My question is "how does the grammar as currently written, arrive at that interpretation"? Where does it say in the grammar that foobar token is WORD. Rule 7b which applies here, does not say that. – Mark Galeck Aug 5 '16 at 8:44
  • ?? thank you for your edit - but I know this... one token is FOO=bar and the second is foobar. My question is, where in the grammar does it say that foobar is a WORD. Rule 7b as currently written, which applies to the cmd_word production, does not say this is WORD. – Mark Galeck Aug 5 '16 at 8:50
  • You're starting your criteria too far down; 7 begins with "[Assignment preceding command name]" – Thomas Dickey Aug 5 '16 at 8:53
  • I am not "starting" looking at any rules by themselves... I am starting by looking at the yacc grammar - then trying the production rule for cmd_word, the only one applicable here, and THAT is what tells me to go to rule 7b. By the way, the label "Assignment preceding command name" is bogus, completely misleading. – Mark Galeck Aug 5 '16 at 8:55
  • also... "too far down", should really be "too far up" if anything - because it says, to apply the highest numbered rule, not the rules in order. I am baffled by this myself - but it clearly says "highest". – Mark Galeck Aug 5 '16 at 9:17

I believe that this address directly to what you ask:

2.10.2 Shell Grammar Rules

  1. [Command Name]

    When the TOKEN is exactly a reserved word, the token identifier for that reserved word shall result. Otherwise, the token WORD shall be returned.

That is rule 1, long before rule 7. Any single word (that contains no =) will be interpreted as a "command name". If it contains an =, rule number 7 is applied.

It may be argued that rule 1 does not have the exception for the inclusion of an =, but I guess that that should be assumed from the description of "Simple Commands":

2.9.1 Simple Commands

A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.

In short: 'variable assignments' 'words' 'redirections'

And: 'variable assignments' shall contain an =.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.