How do I ignore special characters in sed?

I have the following code:

ToString="java.extended.properties=-XX\:MaxPermSize\=128m -XX:CompileCommand=exclude,com/tibco/repo/RVRepoProcessBridge,handleServerHeartbeat"
sed -i "s@.*java.extended.properties.*@$ToString@g" "$ToFile"

It does amend the ToFile but the string doesn't have the 2 \'s in it. How do I ensure that these are recognized as part of the string?


Try escaping the '.' in the sed regex, to be more accurate.

And for the ToString variable:

  1. escape the '\' so that shell interpreter will expand to a single slash (in the third point below),
  2. when defining this variable, use single quotes so it is not expanded, and
  3. expand (by double quoting) when using it within sed, to get the correct end result.

So, something like this should work:

ToString='java.extended.properties=-XX\\:MaxPermSize\\=128m -XX:CompileCommand=exclude,com/tibco/repo/RVRepoProcessBridge,handleServerHeartbeat'

sed -i "s@.*java\.extended\.properties.*@""$ToString""@g" "$ToFile"