Pattern substitute for "\"

Hi
I want to convert string

text="/opt/string/text"

to

"\/opt\/string\/text"

Any ideas how i can do it inside a shell script

I tried sed/awk but i am getting syntax error.
this is code
text =`echo $text | sed 's/\//\\\//'`

HELP

Thanks

You might want to search this site - some of the experts on sed have answered questions like this - it may help.

Being an non-expert with sed, the closest it would do for me is the first /.

echo $text | sed 's/\//\\\//'

I thought it was suppose to go through the whole line replacing all occurances. (Maybe the experts can answer this too). And it would not work in a script...strange.

First thing I would suggest is to use a different character for your forward slashes as sed dividers. If you are using filenames and they contain /'s....you can use = or others ....i.e

Script:
text="/opt/string/text"
echo $text
text=`echo $text | sed 's=/=\\\/=g'`
echo $text

Notice the 'g' on the end (globally do this). Without 'g' you're looking at the first instance of this for each line.

You need 3 backslashes in the 'to' part of the sed command. As you know the \ means take next char literally...so it's basically \\ for the backslash...the \/ for the forward slash.

got it. Thanks.