[Top][All Lists]

[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 (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."

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

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

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.


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

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

reply via email to

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