How to esceape an apostrophe in a shell script using mdfind?

I’m using a shell script and mdfind to locate files that contain a certain tag. It fails when the file tag contains an apostrophe. I’ve tried escaping the apostrophe, but it doesn’t help. Here’s an example of a search

mdfind -onlyin /Users/me/Documents/PDFs ‘kMDItemUserTags = "joe’s"cd’

This also fails:

mdfind -onlyin /Users/me/Documents/PDFs ‘kMDItemUserTags = "joe's"cd’

The error is:

unexpected EOF while looking for matching `"’

so obviously the apostrophe isn’t escaped and is .

Any ideas?

It looks like the forum changed your code, I used yes'icon"19.jpg as the example and this works:
mdfind -onlyin . -name 'yes'\''icon"19.jpg'

Which is why using code tags, like you did, is so helpful.

To expand on @AlbertoD 's answer, anything between the apostrophes is literal so you have to exit the string with an apostrophe, add an escaped apostrophe, then resume the string.

echo 'string'\''s for "you"'
> string's for "you"

Or you could flip it and use double-quotes around the whole string and escape any double-quotes within the string:

echo "string's for \"you\""
> string's for "you"

Thank you both. I’ll give it a try and report back.

Works like a charm. Thanks again.