In [this question][1] someone reports a problem using a [here document][2] with a quoted delimiter word inside [`$(...)` command substitution][3], where a backslash `\` at the end of a line inside the document triggers [newline-joining line continuation][4], while the same here document *outside* command substitution works as expected.

Here is a simplified example document:

 cat <<'EOT'
 abc ` def
 ghi \
 jkl
 EOT

This includes one backtick and one backslash at the end of a line. The delimiter is quoted, so no expansions occur inside the body. In all Bourne-alikes I can find this outputs the contents verbatim. If I put the same document inside a command substitution as follows:

 x=$(cat <<'EOT'
 abc ` def
 ghi \
 jkl
 EOT
 )
 echo "$x"
then they no longer behave identically:

* `dash`, `ash`, `zsh`, `ksh93`, BusyBox `ash`, and SunOS 5.10 POSIX `sh` all give the verbatim contents of the document, as before.
* Bash 3.2 gives a syntax error for an unmatched backtick. With matched backticks, it attempts to run the contents as a command.
* Bash 4.3 collapses "ghi" and "jkl" onto a single line, but has no error. The [`--posix` option][5] does not affect this.

---

In the original question, I said this was a bug in Bash's parser. Is it? The relevant text from POSIX (all from the Shell Command Language definition) that I can find is:

* [§2.6.3 Command Substitution][3]:
 > With the $(command) form, all characters following the open parenthesis to the matching closing parenthesis constitute the command. Any valid shell script can be used for *command*, except a script consisting solely of redirections which produces unspecified results.
* [§2.7.4 Here-Document][2]:
 > If any part of *word* is quoted, the delimiter shall be formed by performing quote removal on *word*, and the here-document lines shall not be expanded.
* [§2.2.1 Escape Character (Backslash)][4]:
 > If a &lt;newline&gt; follows the &lt;backslash&gt;, the shell shall interpret this as line continuation. The &lt;backslash&gt; and &lt;newline&gt; shall be removed before splitting the input into tokens.

My interpretation of this is that all characters after `$(` until the terminating `)` comprise the shell script, verbatim; the here document then has a quoted delimiter, meaning that its contents is processed verbatim; and the escape character never comes into it. I can see an argument, however, that this case is simply not addressed, and both behaviours are permissible. It's possible that I've skipped over some relevant text somewhere, too.

---

* Is this situation made clearer elsewhere?
* What should a portable script be able to rely on (in theory)?
* Is the specific treatment given by of any of these shells (Bash 3.2/Bash 4.3/everyone else) mandated by the standard? Forbidden? Permitted?

 [1]: http://unix.stackexchange.com/q/340718/73093
 [2]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04
 [3]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03
 [4]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02_01
 [5]: https://www.gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode