From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17780 invoked from network); 7 Feb 2002 21:00:17 -0000 Received: from sunsite.dk (130.225.247.90) by ns1.primenet.com.au with SMTP; 7 Feb 2002 21:00:17 -0000 Received: (qmail 210 invoked by alias); 7 Feb 2002 21:00:09 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 16589 Received: (qmail 169 invoked from network); 7 Feb 2002 21:00:08 -0000 Date: Thu, 7 Feb 2002 21:00:05 +0000 (GMT) From: Bart Schaefer Sender: lantern@brasslantern.com To: Derek Peschel cc: zsh-workers@sunsite.dk Subject: Re: BUG? - 4.0.2 - parameter substitution won't double backslashes in values In-Reply-To: <20020207122222.A14893@eskimo.eskimo.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII On Thu, 7 Feb 2002, Derek Peschel wrote: > Maybe my explanation was too complicated, or probably you missed the > beginning of the thread. I saw the beginning of the thread, and I saw Sven's answer, which didn't seem to bear repeating, so I was responding only to the parenthetical comment about backspace changing to "\b". As Sven's answer apparently does bear repeating: > I have a string containing the characters "a", backslash, "b", "c". [...] > I want to use parameter substitution to convert the backslash to two > backslashes. You *probably* want the (q) parameter flag: zsh% x='a\bc' zsh% print ${(q)x} a\bc zsh% print -r ${(q)x} a\\bc However, (q) will also insert a backslash in front of any other character that is special to the shell parser. If you want *only* to double all the backslashes, you need one of: zsh% print -r ${x//\\\/\\\\} a\\bc zsh% print -r ${x:gs/\\/\\\\\\\\} a\\bc The reason you need three backslashes as the pattern in the first case is rather complicated and could possibly be considered a bug; it has to do with using glob-pattern interpretation in ${x//...}. The reason you need eight backslashes as the replacement in the second case is a lot easier to explain; the eight are reduced to four by the initial parse of the shell command line, and then reduced again to two when the :gs replacement occurs. The second one is probably more reliable, as it works the same even if the expansion is enclosed in double quotes.