You are not logged in.

#1 2011-02-18 15:19:15

synthead
Member
Registered: 2006-05-09
Posts: 1,337

Bash: "${foo/\"/'}" fails

Say foo="Flabby Jiggleson\"s files".  You want to replace the " with a '.  Naturally, with a parameter expansion, you would

mv "$foo" "${foo/\"/'}"

Easy, right?  Well, it fails, giving me a carat as if I was quoting a newline.  So I try escaping it...

mv "$foo" "${foo/\"/\'}"

And I get...

Flabby Jiggleson\'s files

...with the backslash in it.

So ... now what?

Offline

#2 2011-02-18 15:38:45

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Bash: "${foo/\"/'}" fails

This is a little ug-bug:

$ mv "$foo" "$(echo -e "${foo/\"/\047}")"

Offline

#3 2011-02-18 15:46:43

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Bash: "${foo/\"/'}" fails

I asked in #bash; this is better

q1=\' q2=\";... "${foo/$q2/$q1}"

Offline

#4 2011-02-18 16:47:51

synthead
Member
Registered: 2006-05-09
Posts: 1,337

Re: Bash: "${foo/\"/'}" fails

Hmm, interesting.  Any way I could pass a literal ' ?  $'\'' style?

Offline

#5 2011-02-18 17:49:48

freak
Member
Registered: 2009-04-15
Posts: 17

Re: Bash: "${foo/\"/'}" fails

No, the reason it acts that way has to do with POSIX compatability, I believe.  The best way is indeed to use a variable.

q=\'; mv "$foo" "${foo/\"/$q}" # would be how I would do it.

Offline

#6 2011-02-19 05:15:07

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: Bash: "${foo/\"/'}" fails

Works for me... Am I missing something?

/home/fukawi2 $ FOO="I said \"Moo\" to you"
/home/fukawi2 $ echo $FOO
I said "Moo" to you
/home/fukawi2 $ echo ${FOO//\"/\'}
I said 'Moo' to you

EDIT: Interesting, if I quote it, then it breaks:

/home/fukawi2 $ echo "${FOO//\"/\'}"
I said \'Moo\' to you

Last edited by fukawi2 (2011-02-19 05:16:14)

Offline

#7 2011-02-19 17:51:08

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: Bash: "${foo/\"/'}" fails

This is really weird behaviour, and almost seems like a bug if freak isn't right about it being a POSIX compatibility artifact.  For the record, in zsh, "${foo/\"/'}" works, as I think it should in bash.  "${foo/\"/\'}" gives an extra backslash in both shells, which also makes sense to me (since echo "\'" prints a backslash too).

Offline

Board footer

Powered by FluxBB