Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to convert all apostrophes in this file to X:

Bob's book
Bob’s book
Bob′s book  # (Might look the same but actually different)

The first apostrophe is replaced as expected:

$ cat file | tr "'" "X"
BobXs book
Bob’s book
Bob′s book

But the the two other kinds of apostrophe, strange things happen:

$ cat file | tr "’" "X"
Bob's book
BobXXXs book
BobXX�s book

$ cat file | tr "′" "X"
Bob's book
BobXX�s book
BobXXXs book

How to make it work?

share|improve this question
up vote 6 down vote accepted

tr works in units of bytes, which means it doesn't work properly for multi-byte encodings like UTF-8. The only solutions I know of are to find a version of tr that supports Unicode, or switch to sed or some other tool that can do string replacement.

share|improve this answer
sed -e "s/’/X/" works. – Nicolas Raoul Sep 19 '12 at 3:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.