I am trying to understand the POSIX shell standard here

From reading it, appears to me in the following shell command:

echo $(FOO=bar foobar)

there are two tokens (for the top level shell, not the subshell): echo and $(FOO=bar foobar)

This is a useful type of command and not contrived at all, so it should be defined what the results are. But when you try to parse it using the grammar given in the standard, the second token is unspecified by Rule 7b of the grammar - because it contains = but before that, is not a valid name.

Are the results of such a "normal" command really not specified? Or what am not understanding??

  • 7b only applies to words before the first WORD. Here, the first WORD is echo. The question would still stand for $(foo=bar; echo echo) bar – Stéphane Chazelas Aug 5 '16 at 9:04
  • @StéphaneChazelas I respectfully disagree - from the grammar, clearly, 7b applies to words after the first one (there is nothing before the first one, by definition). – Mark Galeck Aug 5 '16 at 9:07
  • That's about Assignment preceding command name. – Stéphane Chazelas Aug 5 '16 at 9:08
  • @StéphaneChazelas yes I know :) that label is misleading and should be disregarded - note that this rule applies, from the productions for cmd_word and cmd_name to command name itself, and in the rule itself, also talks about command names. – Mark Galeck Aug 5 '16 at 9:11
  • 2
    I agree it's unclear. I would suggest you ask those questions on the austin group mailing list. (that list relied on gmane for the archives, I'm not sure where to find them now that gmane web interface is gone). – Stéphane Chazelas Aug 5 '16 at 9:40

Yes, echo $(FOO=bar foobar) has two tokens: echo and $(FOO=bar foobar).

The first token is a command, the second one is an expansion $(…).

The command is recognized in step 1

The expansion is recognized in step 5

If the current character is an unquoted '$' or '`', the shell shall identify the start of any candidates for parameter expansion (Parameter Expansion), command substitution (Command Substitution) …

A "Command substitution" to be more precise:

… Command substitution shall occur when the command is enclosed as follows:


The shell shall expand the command substitution by executing command in a subshell environment and replacing the command substitution (the text of command plus the enclosing "$()" or backquotes) with the standard output of the command

With the $(command) form, all characters following the open parenthesis to the matching closing parenthesis constitute the command.

It is therefore clear that the command to be executed in a sub-shell would be:

FOO=bar foobar

Which also has two tokens: a variable assignment (FOO=bar) and a command (foobar).

The result of executing such command will replace the whole $(…).

That is all valid POSIX syntax, I fail to see any issue.

  • you don't understand my question. I am not at all asking how this works, which you spend the whole answer explaining. I know this, you don't have to explain. There is one bit in your answer that relates to my question: you are saying this is valid POSIX. How is it valid, this is my question - how is the grammar accepting the second token `$(FOO=bar foobar). Just tell me precisely how does it do that? – Mark Galeck Aug 6 '16 at 2:29
  • 1
    You don't have to be patronizing. Never mind. I just found out, from the POSIX Standard Group, this is a known issue and they have approved a fix, but has not been published yet. Have a nice day. – Mark Galeck Aug 6 '16 at 7:04

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.