I'm writing a shell, and am getting unexpected parsing from both bash, dash, and busybox's ash:
echo "`echo a #`"
prints a
, however
echo "$(echo a #)"
gives an error about missing a closing )
.
How is a comment in a command-substitution parsed according to POSIX? So, for the commands:
echo "`echo a #`"
and
echo "$(echo a #)"
Will the shell parse the comment as extending to the end of the command substitution, or to the end of the line? Also, will the shell parse it differently if the command substitutions are not in double quotes? Finally, are there any other constructs (either in POSIX or bash) where a comment can start inside quotes like this?
According to Posix (Shell&Utilities, §2.6.3), "`echo a #`"
is undefined (implying that it should not be used):
The search for the matching backquote shall be satisfied by the first unquoted non-escaped backquote; during this search, if a non-escaped backquote is encountered within a shell comment, … undefined results occur. (emphasis added)
However, the $(
command substitution marker is terminated by the "first matching )
"; the implication (made explicit by examples in the Rationale, Note 1) is that the matching )
cannot be inside of a shell comment, here-doc or quoted string.
The quotes surrounding the command substitution are not relevant in either case (although, of course, "undefined results" could be different in the quoted case, since they are undefined.)
In bash and certain other shells, comments could also be present inside process substitution (eg., <(…)
); however, process substitution cannot be quoted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With