How do I escape double and single quotes in sed?
The s///
command in sed
allows you to use other characters instead of /
as the delimiter, as in
sed 's#"http://www\.fubar\.com"#URL_FUBAR#g'
or
sed 's,"http://www\.fubar\.com",URL_FUBAR,g'
The double quotes are not a problem. For matching single quotes, switch the two types of quotes around. Note that a single quoted string may not contain single quotes (not even escaped ones).
The dots need to be escaped if sed
is to interpret them as literal dots and not as the regular expression pattern .
which matches any one character.
Regarding the single quote, see the code below used to replace the string let's
with let us
:
command:
echo "hello, let's go"|sed 's/let'"'"'s/let us/g'
result:
hello, let us go
My problem was that I needed to have the ""
outside the expression since I have a dynamic variable inside the sed expression itself. So than the actual solution is that one from lenn jackman that you replace the "
inside the sed regex with [\"]
.
So my complete bash is:
RELEASE_VERSION="0.6.6"
sed -i -e "s#value=[\"]trunk[\"]#value=\"tags/$RELEASE_VERSION\"#g" myfile.xml
Here is:
#
is the sed separator
[\"]
= "
in regex
value = \"tags/$RELEASE_VERSION\"
= my replacement string, important it has just the \"
for the quotes