Subject: Re: bug#11991: tr bug

From: Bob Proulx <[email-masked]>
Date: Thu, 19 Jul 2012 10:38:42 -0600

retitle 11991 shell file globbing confusion
tag 11991 + notabug
close 11991
thanks

Xiao, Bellon (NSN - CN/Cheng Du) wrote:
> We found a bug of tr, the version is tr (coreutils) 5.2.1

Thank you for your report.  But this is not a bug in tr but simply a
misunderstanding of how your command line shell is working.  It is a
behavior of the shell and doesn't have anything to do with tr.

> Here is the symptom:
> When there is a file named e or r under current directory,

Here you have made the critical description saying that there are
files in the current directory.  Shell file globbing is matching those
files.  You need to quote your arguments to prevent this.

> tr will take effect，like：
> 	echo hello |tr [a-z] [A-Z] 
> 	echo hello |tr [:lower:] [:upper:]
> will return hello

Those command lines are insufficiently quoted and therefore incorrect.

> But if you try 
> 	echo kkhh |tr "[a-z]" "[A-Z]" 
> 	echo kkhh |tr "[:lower:]" "[:upper:]"
> There will be no error and it takes effect.

And there you see that you are quoting the arguments to prevent the
shell from expanding them.

Also the collating sequence depends upon your specific locale.
The collation sequence of [a-z] in dictionary ordering is really
"aAbBcC...xXyYzZ" and not "abc...z".  So when you say "[a-z]" you are
getting "aAbBcC...xXyYz" without 'Z' and when you say "[A-Z]" you are
really getting "AbBcC...xXyYzZ" with 'A'!

In the en_US.UTF-8 locale (for example) what would traditionally have
been [A-Z] and [a-z] now must be specified as [:upper:] and
[:lower:] instead.  (And for grep, sed, awk and other tools you would
need to put those in a character class [...] so you want two brackets
there with [[:lower:]] and [[:upper:]].)

Instead of 'tr' try using 'echo' to see what command line you are
actually passing to the tr command.

  $ echo tr [a-z] [A-Z]

  $ echo tr [:lower:] [:upper:]

  $ echo tr "[a-z]" "[A-Z]"

  $ echo tr "[:lower:]" "[:upper:]"

By running the above commands in directories containing single
characters and you won't see the arguments verbatim to the command
unless you quote them.

Since this concerns the shell and not tr I am going to go ahead and
mark the bug as closed to clear up the accounting.  But please feel
free to continue discussing the issue.

Bob