Edit to clarify my question:
POSIX says:
If a <newline> follows the (unquoted) <backslash>, the shell shall interpret this as line continuation. The <backslash> and <newline> shall be removed before splitting the input into tokens.
However, dash
or other implementations, tokenize input at first. As a result, \<newline>
is not recognized but # this is a comment \
is discarded.
Is this behavior POSIX compliant? Again, POSIX says that line continuation shall be removed before tokenizing.
Isn't the following procedure really POSIX compliant?
- read the whole input:
"echo hello ... \<newline> ... bye"
- search for unquoted
\<newline>
and remove them:"echo hello ... bye"
- tokenize:
"echo"(discard ' ')"hello"(discard ' ')(discard "# ... bye")
On Ubuntu with dash-0.5.10.2-6 sh (dash) we get the following
$ cat /var/tmp/test.sh
echo hello # this is a comment \
echo bye
$ sh /var/tmp/test.sh
hello
bye
This is because everything after # is treated as a comment, and everything up to \ is discarded, so line continuation of \<newline> does not work.
However, POSIX "Escape Character (Backslash)" section states
The <backslash> and <newline> shall be removed before splitting the input into tokens.
And since comment processing of # is done in tokenization,
echo hello # this is a comment \
echo bye
should be equivalent to
echo hello # this is a comment echo bye
Does this mean that sh is not POSIX compliant? Or is there some rationale for comment taking precedence over line continuation in this situation?
echo foo<backslash><newline>bar
printsfoobar
, notfoo bar
)