Thanks for your reply.
Description:
Bash allows escape characters to be embedded by using the $'\n'
syntax. However, unlike all other $variables,
this doesn't work with embedded newlines. I think it should.
Repeat-By:
X="a$'\n'b c"
echo "$X"
expect to see:
a
b c
actually see:
a$'\n'b c
Fix:
$'\n' should be expanded within double-quotes, like other variables are.
Otherwise, please correct the man-page to make it clearer.
$'\n' is not a variable. As the man page says:
Words of the form $'string' are treated specially.
Note "Words". Inside double quotes, $'\n' is not a word.
I agree that this is technically correct. However, it violates the
principle of least-surprise, which is that, in bash, the $ symbol always
expands the value of the thing after it, (with the exceptions of
'$' and \$ .)
(On re-reading the man-page, I agree that the documentation is
consistent with your explanation; though it still appears more likely to
imply mine)
If this is a feature, not a bug, then is there a better way to include
newlines in a variable-assignment?
The syntax X="a"$'\n'"b c" will do it, but that is really really
ugly.
X=$'a\nb c'
This is still a missing feature: how to embed newlines in double-quoted
bash string assignment:
For example, if I want to write:
EMAIL_BODY="Dear $NAME,$'\n\n'Here are the log-files for
$(date)$'\n\n'Regards,$'\n\n'$SENDER"
then this doesn't work. There are ways around it, such as:
- building up the string in pieces or
- EMAIL_BODY=$(echo -e "$EMAIL_BODY")
but it's really ugly to do.
As I imagine that nobody uses the current $'\n' inside double-quotes,
may I request this as a functionality change?
Best wishes,
Richard