[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Including ' in a sed command



On Fri, Aug 08, 2025 at 16:05:45 +1000, Eyal Lebedinsky wrote:
> On 8/8/25 12:17, Tim Woodall wrote:
> > sed 's/["'"'"']//'
> > Is there a better way of writing it? the obvious '["\']' doesn't work.

The canonical POSIX ways would be:

    sed 's/["'\'']//'
    sed "s/[\"']//"

> These work with bash for me (bash). I usually prefer to enclose in dq since I often have shell vars in the rule:
> 	sed "s/[\"']//"
> 	sed $"s/["\']//"

The first one is correct (and isn't bash-specific).  The second one
isn't correct.  It should be:

    sed $'s/["\']//'

That, of course, is a bashism.  However, since we're just deleting single
characters, we could use tr instead:

    tr -d \"\'

That would be my preference for this *particular* task.

hobbit:~$ echo $'Isn\'t it "great"?' | tr -d \"\'
Isnt it great?


Reply to: