bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: $\n doesn't get expanded between double-quotes


From: Chris F.A. Johnson
Subject: Re: $\n doesn't get expanded between double-quotes
Date: Fri, 3 Jul 2009 13:01:47 -0400 (EDT)
User-agent: Alpine 2.00 (LMD 1167 2008-08-23)

On Fri, 3 Jul 2009, Richard Neill wrote:

> > > 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.

   You can include literal newlines inside double quotes:

EMAIL_BODY="Dear $NAME,

Here are the log-files for $(date)

Regards,

$SENDER"


   You can assign a newline to a variable and include that:

NL=$'\n'
string="This is a$NLmulti-line$NLstring"
 
   There are other ways as well.

> As I imagine that nobody uses the current $'\n' inside double-quotes, may I
> request this as a functionality change?

-- 
   Chris F.A. Johnson, webmaster         <http://woodbine-gerrard.com>
   ===================================================================
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]