sed within `` in Bourn Shell isn't Working.

Lorenzo Luengo lluengo at dgeo.udec.cl
Mon Mar 23 16:22:12 UTC 2009


Smoot Carl-Mitchell escribió:
> On Sat, 2009-03-21 at 17:16 -0500, Martin McCormick wrote:
>   
>> J
>> 	It worked exactly the same way for me. Thank you. I
>> think what we gain here is that we are not running a subshell.
>> As to why you need 6 \ characters in order to get 2 to print
>> when one would expect to have to only use 4, I am not totally
>> sure either, but just for laughs, I tried 8 \'s and still only
>> got 2 out. Ten of them did give me 3 out with 4 mystery
>> sacrificial backslashes to the shell god.:-) It is as if some
>> sort of recursion is going on but I don't really understand why.
>>     
>
> You can see what is happening by setting the -x option in the shell.
>
> set -x
> uid=`echo 'ABC\DEF' | sed 's/\\\/\\\\\\\/g'`; echo $uid
> ++ sed 's/\\/\\\\/g'
> ++ echo 'ABC\DEF'
> + uid='ABC\\DEF'
> + echo 'ABC\\DEF'
> ABC\\DEF
>
> This with 7 backslashes in the target of the sed substitution.  What
> appears to be happening here is the parent shell is doing the backslash
> substitution.  With 3 backslashes you get 2 emitted.  With 7 you get 4.
> I find this a little odd because I would have expected the string '\\\/'
> to emit '\/' and not '\\/'.  This could be a shell feature (bug).  It
> looks like using the the $() is safer and more consistent.
>   
echo `echo -n 'ABC\DEF' | sed 's/\\\/\\\\\\\\/g'`

Enough backslashes??
3 for the first: shell yields a backslash for the 2 first, so sed's 
regex is '\\' (one backslash), maybe placing 4 is more correct.
8 for the second: shell yields one backslash for every two found 
('\\\\') which is a fine representation for the replacement. 7 also 
work, because shell doesn't escape the last one.

Nice excercise thinking on this too many backslashes.

-- 
Lorenzo Luengo Contreras
Administrador de Sistemas DGEO
Universidad de Concepción
Concepción - Chile
+56-41-2207277





More information about the ubuntu-users mailing list