Reputation: 28587
This:
echo "Hel'lo" | sed s/\'/\\\'/g
Yields
Hel'lo
What I want is this:
Hel\'lo
What am I missing?
Upvotes: 4
Views: 35055
Reputation: 46846
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
Upvotes: 1
Reputation: 13906
Or without quoting the sed argument:
echo "Hel'lo" | sed s/\'/\\\\\'/g
Upvotes: 1
Reputation: 31451
echo "Hel'lo" | sed "s/'/\\\'/g"
Hel\'lo
Also
echo "Hel'lo" | sed s/\'/\\\\\'/g
Upvotes: 14