This:
echo "Hel'lo" | sed s/\'/\\\'/g
Yields
Hel'lo
What I want is this:
Hel\'lo
What am I missing?
4 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/'\''/\\&/'