tr on OS X

Brandon Allbery allbery at
Sat Apr 3 01:00:57 PDT 2010

On Apr 3, 2010, at 03:46 , Scott Haneda wrote:
> Just out of curiosity, if I wanted to do something like:
> $cat /dev/random | tr "a" "b"
> How do I get `tr` to work?  I do not see anything in the man page  
> that would allow me to treat the binary data as correctly.

$ LANG=C tr a b </dev/random

If a multibyte localization is configured (OSX and most Linuxes  
default to UTF8, which is a variable length encoding) then tr will  
become annoyed when it sees illegal character sequences.  In single- 
byte encodings like ISO8859/1 you're probably fine as long as you  
don't use things like [:upper:], but LANG=C disables all of this and  
processes the data as a raw stream of bytes.

tr on many systems was until recently not locale-aware because it was  
designed internally to deal with bytes, not characters.  Recent GNU  
tr, and BSD tr (which is what OSX uses), were rewritten to be locale- 
capable. and therefore trip over the illegal-UTF8-sequences problem  
when used on binary data unless LANG=C is used to disable all locale  

brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at
system administrator [openafs,heimdal,too many hats] allbery at
electrical and computer engineering, carnegie mellon university    KF8NH

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
URL: <>

More information about the macports-users mailing list