sed replace ' with \'

This:

echo "Hel'lo" | sed s/\'/\\\'/g

Yields

Hel'lo

What I want is this:

Hel\'lo

What am I missing?

Answers


echo "Hel'lo" | sed "s/'/\\\'/g"
Hel\'lo

Also

echo "Hel'lo" |  sed s/\'/\\\\\'/g

Or without quoting the sed argument:

echo "Hel'lo" | sed s/\'/\\\\\'/g

You can also do it with all single quotes:

echo "Hel'lo" | sed 's/'\''/\\'\''/g'

And since your question is also tagged bash, I might as well point out that you don't even need sed:

[ghoti@pc ~]$ foo="Hel'lo"
[ghoti@pc ~]$ echo "${foo/\'/\'}"
Hel\'lo

This might work for you:

echo "Hel'lo" | sed 's/'\''/\\&/'

Need Your Help

How to rotate embedded youtube video to landscape mode

ios youtube embed landscape uiinterfaceorientation

I am using the following embed code to embed my youtube videos on IOS

Find all points with integer coordinates inside tetrahedron

math 3d coordinates interpolation

I am trying to find all the points with integer coordinates that lie inside of a tetrahedron (I want to somehow be able to loop through them). I know the coordinates of the four points (A, B, C, D)...