|
From: | Richard Neill |
Subject: | Re: $\n doesn't get expanded between double-quotes |
Date: | Fri, 03 Jul 2009 17:47:42 +0100 |
User-agent: | Thunderbird 2.0.0.21 (X11/20090330) |
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
[Prev in Thread] | Current Thread | [Next in Thread] |