understanding the sed command

Guys,

I am trying to understand the sed command here.

adx001 $ a=/clocal/dctrdata/user/dctrdat1/trdroot/recouncil
adx001 $ b=`echo $a |  sed 's/\//\\\\\//g'`
adx001 $ echo $b
\/clocal\/dctrdata\/user\/dctrdat1\/trdroot\/recouncil

The sed command i took it from the script.

Please correct my understanding. As per my understanding, it searches for / (/\) and replaces it with \\/ (\\\\\/).

But the output is having only \/.

Please tell me where i am going wrong.

Thanks for your help in advance.

Magesh

---------- Post updated at 08:18 PM ---------- Previous update was at 07:04 PM ----------

Guys found the prob,,
it is because of the echo statement

echo $b

Where the first "/" is taken as escape character.

No, it seaches for each occurance of '/' and replaces it with '\\/'

$ echo $a |  sed 's|/|\\\\/|g'
\\/clocal\\/dctrdata\\/user\\/dctrdat1\\/trdroot\\/recouncil

Note the difference of the two equivalent entries:

a=/clocal/dctrdata/user/dctrdat1/trdroot/recouncil
b=`echo $a |  sed 's/\//\\\\\//g'`
echo $b
\/clocal\/dctrdata\/user\/dctrdat1\/trdroot\/recouncil
echo $a | sed 's/\//\\\\\//g'
\\/clocal\\/dctrdata\\/user\\/dctrdat1\\/trdroot\\/recouncil

Don't know exactly how to explain it but it seems that when the variable is assigned, there is an extra level of escape that is happening when echoing that variable which returns the different result.

peterro and fmurphy, both of you are correct. when the value is assigned to the variable b, it will be \\/clocal\\/dctrdata\\/user\\/dctrdat1\\/trdroot\\/recouncil.

But because of the echo statement below, one of the \ is taken as escape character.

if we do

d02 $echo \\/
\/

This is what is happening.