Expanding a little bit on what RudiC said in post #4 in this thread, shell variable expansions do not occur in single-quoted strings. What chandan.chaman suggested in post #5 would also work, but is more complicated than is needed.
Are backslash characters the only characters that are special in basic regular expressions that might appear in the text you want to replace? (Note that other BRE special characters include the period, opening square bracket (i.e., [ ), asterisk, circumflex, and dollar sign).
Are you creating the search pattern and replacement strings (so you can escape "special" characters)? Or, are they created by users invoking your script?
Does the pattern you want to replace in your file only occur once in your file? Or, can it appear on several lines in your file? Can it ever occur more than once on a single line in your file (and need to have every occurrence replaced)? If the pattern is to be treated as a literal string and doesn't need to be replaced more than once on any line, you might want to use awk 's index () function to find your pattern and use substr () a couple of times to do the replacements instead of using sed 's or ed 's substitute command or awk 's sub () or gsub () function to replace your text.