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

Smoot Carl-Mitchell smoot at tic.com
Sat Mar 21 23:42:56 UTC 2009


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.
-- 
Smoot Carl-Mitchell
Computer Systems and
Network Consultant
smoot at tic.com
+1 480 922 7313
cell: +1 602 421 9005




More information about the ubuntu-users mailing list