bug-bash
[Top][All Lists]
Advanced

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

Re: One cant insert a single quote into a variable with built-in substit


From: Chet Ramey
Subject: Re: One cant insert a single quote into a variable with built-in substitution.
Date: Fri, 01 Aug 2008 22:34:42 -0400
User-agent: Thunderbird 2.0.0.16 (Macintosh/20080707)

Damien Nadé wrote:

I've tried to insert a single quote into a variable content. With something like this :
bash-3.2$ foo=bar
bash-3.2$ echo "${foo/%/'}"

If you look at that, you understand that > is the $PS2, so it means that
bash is interpreting the single quote a special char.

Bash is acting as Posix specifies.  When parsing a ${...} expansion
within double quotes, "an even number of unescaped double-quotes or
single-quotes, if any, shall occur."
(http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_02_03)

So, naturally, I've told myself : "just escape it". And I've tried
that :

bash-3.2$ foo=bar
bash-3.2$ echo "${foo/%/\'}"
bar\'

But... what the hell ? The backslash belongs to the variable now ?

I believe this is right, too.  The single quote isn't one of the
characters for which backslash retains its special meaning within
double quotes, so the backslash is retained by the expansion and
appended to the expanded value of foo as specified by the pattern.

The suggestion to use an intermediate variable works.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer

Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

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