sed variable and backslash

I am trying to write a bash shell script, which extracts a sentence from a dynamically created dynamic file and passes it as a variable to sed and uses that sentence as a starting point to pull the content from a log file.

the key part of the script is this

key=`cat /tmp/dynamic`

sed -n "/$key/,\$p" /logfile

the problem is, when the sentence has backslash in it, it totally screws the sed command, for example, if the sentence is "02/16/2010 06:43:33 PM CST : CRITICAL ERROR", when it gets passwd to sed, sed will see the backslash and that will cause problem,

sed -n "/02/16/2010 06:43:33 PM CST : CRITICAL ERROR/,\$p'

Anyone knows how can i pass backslash to sed as a variable in this case? thanks

I see only normal slashes in your example,
If you want to escape backslash then use another backslash in front of it,
Or If you have many slash in the patterns like in your example then use the below form,then as i know you dont need to escape slashes:

sed -n '# # #' logfile

ok, let me give a good example:

assume you want to change the directory /usr/bin/sh with /usr/bin/bash then use this

sed 's#/usr/bin/sh#/usr/bin/bash#g'  file

otherwise you have to use many backslashes to escape slashes:

sed 's/\/usr\/bin\/sh/\/usr\/bin\/bash/g' file

sorry i mean slash, not backslash,

i know i can use blackslash to escape slash, but the thing is i have to pass the variable to sed. and i cannot predict if the variable will have slash and where the slash will show up.

Be aware that your approach, expanding a variable into a sed regular expression, can cause problems with other special characters, not just the slash. If it's a special regular expression character (., *, etc) and it appears in the variable's value, the script will not work correctly.

If forward slash is the only thing you're concerned about, Eagle's suggestion of using a different delimiter is the simplest solution (of course, if the chosen delimiter appears in the variable's value, you're back to square one). If other special characters may occur, perhaps AWK and non-regular expression substring operations would be wiser.

awk -v k="$key" 'index($0,k){i=1}i'