When run in bash, this command:
cat <<EOF
hello \\
world
EOF
will output as I expect:
hello \
world
But this one outputs hello \world
(without the newline), and is unexpected:
echo "$(cat <<EOF
hello \\
world
EOF
)"
If I run the second command with dash, then the output is as expected (with a newline).
Why is the newline removed in 2nd command in bash?
Because you are re-parsing the output again, so the double-backslash has already been parsed, and the now-single backslash is quoting the newline, making it a continuation line.
Take them both out and it works as you'd expect.
See comments below.
I'd delete the answer, but the useful comments would go as well.
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