Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Command line tr behaving different to Linux

I've been writing scripts to automate image building for various embedded software projects, but I found tr is behaving differently on my Mac to my Linux PC.


I'm essentially trying to generate an 0xFF padded image.


Linux yields (as expected):


tr '\000' '\377' < /dev/zero | hexdump

0000000 ffff ffff ffff ffff ffff ffff ffff ffff


But MacOS Terminal gives me:


tr '\000' '\377' < /dev/zero | hexdump

0000000 c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf


I can't figure out exactly what's going on, I guess MacOS is interpreting the octal differently. I have a workaround using Python, but this is bugging me! Can anyone here tell me what's going on?


Thanks

MacBook Pro, OS X Mountain Lion (10.8.2), MacOS Terminal

Posted on Feb 14, 2013 3:52 AM

Reply
28 replies

Feb 14, 2013 8:35 AM in response to FrijolePerezoso

The latest versions of Ubuntu and Fedora use GNU tr. Mac OS X (Mountain Lion) uses the FreeBSD version. And I expect that hexdump has those pedigree as well, on the respective systems.


I get the same results as the OP and all Zeros from Linc's suggestion.


Downloading and making a special build of GNU tr from the GNU Coreutils package is a lot more work than a working Python solution. Better still would be finding the desired result from the FreeBSD tr.


Been experimenting, but nothing so far that returns two-byte ff with hexdump -x.

Feb 14, 2013 11:28 AM in response to Linc Davis

Cut and paste from your terminal window the command that is working. So far three of us have all said it isn;t so you must be doing something different or else you are not using the same tr we all are.


The standard tr is in /usr/bin when I do a which. Also what shell are you using. I'm using bash


Here is my standard tr output

/usr/bin/tr '\000' '\377' < /dev/zero | hexdump

0000000 c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf

*

^C


Message was edited by: Frank Caggiano - Added terminal line

Feb 14, 2013 2:33 PM in response to Linc Davis

Heck if I can figure it out. Maybe a candiate for your puzzle posts!


This tr command is a cut and paste from your post just to rule out any weird hidden chars somewhere


Pandora:Downloads frank$ /usr/bin/tr '\000' '\377' < /dev/zero | hexdump

0000000 c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf c3 bf

*

^C

Pandora:Downloads frank$ uname -v

Darwin Kernel Version 12.2.0: Sat Aug 25 00:48:52 PDT 2012; root:xnu-2050.18.24~1/RELEASE_X86_64

Pandora:Downloads frank$ strings /usr/bin/tr

$FreeBSD: src/usr.bin/tr/cmap.c,v 1.2 2004/07/14 08:36:09 tjr Exp $

$FreeBSD: src/usr.bin/tr/cset.c,v 1.3 2004/07/14 08:33:14 tjr Exp $

$FreeBSD: src/usr.bin/tr/str.c,v 1.24 2004/11/14 05:15:25 jkh Exp $

$FreeBSD: src/usr.bin/tr/tr.c,v 1.24 2005/04/09 14:31:41 stefanf Exp $

@(#)PROGRAM:tr PROJECT:text_cmds-84

cmap_splay

/SourceCache/text_cmds/text_cmds-84/tr/cmap.c

t != NULL

cset_delete

/SourceCache/text_cmds/text_cmds-84/tr/cset.c

cset_rangecmp(t, ch) == 0

cset_splay

genrange() malloc

misplaced sequence asterisk

illegal sequence count

misplaced equivalence equals sign

unknown class %s

upper

lower

Ccdsu

strdup(argv[1])

empty string2

usage: tr [-Ccsu] string1 string2

tr [-Ccu] -d string1

tr [-Ccu] -s string1

tr [-Ccu] -ds string1 string2

rune

Pandora:Downloads frank$

I'll see your uname and raise you a strings 😕


Message was edited by: Frank Caggiano - BTW you never answered the shell you are using as I said I'm bash Just saw in in your last post


bash --version

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)

Copyright (C) 2007 Free Software Foundation, Inc.

[Pandora:~/Downloads] frank%

Feb 14, 2013 2:45 PM in response to Frank Caggiano

If the OP responds, we may find out whether he's using the system tr(1) and hexdump(1). Meanwhile, "bash --version" only tells you what version of bash is installed, not which shell you're now running. You'd get the same output in any shell:


$ zsh

➜ ~ bash --version

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)

Copyright (C) 2007 Free Software Foundation, Inc.

Feb 14, 2013 2:52 PM in response to Linc Davis

Ah right, I'm am running bash for the record. Just for ha-has I ran through all the available shells and ran the command I get the same response in all.


I would be interested in what strings shows for your /usr/bin/tr. The command itself, tr, doesn't respond to -v or --version.


Message was edited by: Frank Caggiano - Just realized I don;t have a BASH_VERSION environmental variable set. Should that be happening automatically or are you setting it and could that indicate something?

Command line tr behaving different to Linux

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.