How to escape single quote in sed?

Thomas Bratt picture Thomas Bratt · Jul 1, 2014 · Viewed 85.3k times · Source

How to escape a single quote in a sed expression that is already surrounded by quotes?

For example:

sed 's/ones/one's/' <<< 'ones thing'

Answer

Kent picture Kent · Jul 1, 2014

Quote sed codes with double quotes:

    $ sed "s/ones/one's/"<<<"ones thing"   
    one's thing

I don't like escaping codes with hundreds of backslashes – hurts my eyes. Usually I do in this way:

    $ sed 's/ones/one\x27s/'<<<"ones thing"
    one's thing