GNU bug report logs - #11991
shell file globbing confusion

Previous Next

Package: coreutils;

Reported by: "Xiao, Bellon (NSN - CN/Cheng Du)" <bellon.xiao <at> nsn.com>

Date: Thu, 19 Jul 2012 16:16:03 UTC

Severity: normal

Tags: notabug

Done: Bob Proulx <bob <at> proulx.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 11991 in the body.
You can then email your comments to 11991 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#11991; Package coreutils. (Thu, 19 Jul 2012 16:16:03 GMT) Full text and rfc822 format available.

Acknowledgement sent to "Xiao, Bellon (NSN - CN/Cheng Du)" <bellon.xiao <at> nsn.com>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Thu, 19 Jul 2012 16:16:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Xiao, Bellon (NSN - CN/Cheng Du)" <bellon.xiao <at> nsn.com>
To: <bug-coreutils <at> gnu.org>
Cc: "Han, Cathy \(NSN - CN/Cheng Du\)" <cathy.han <at> nsn.com>
Subject: tr bug
Date: Thu, 19 Jul 2012 14:38:55 +0800
[Message part 1 (text/plain, inline)]
Hi,

We found a bug of tr, the version is tr (coreutils) 5.2.1
Here is the symptom:
When there is a file named e or r under current directory, tr will take effect,like:
	echo hello |tr [a-z] [A-Z] 
	echo hello |tr [:lower:] [:upper:]
will return hello

And when is a file named l, o, p, u or w under current directory, tr will give error message like:
	echo hello |tr [a-z] [A-Z] 
	echo hello |tr [:lower:] [:upper:]
gives : tr: misaligned [:upper:] and/or [:lower:] construct


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.

Brs
Bellon Xiao


[Message part 2 (text/html, inline)]

Information forwarded to bug-coreutils <at> gnu.org:
bug#11991; Package coreutils. (Thu, 19 Jul 2012 16:46:02 GMT) Full text and rfc822 format available.

Message #8 received at 11991 <at> debbugs.gnu.org (full text, mbox):

From: Bob Proulx <bob <at> proulx.com>
To: "Xiao, Bellon (NSN - CN/Cheng Du)" <bellon.xiao <at> nsn.com>
Cc: 11991 <at> debbugs.gnu.org, "Han,
	Cathy \(NSN - CN/Cheng Du\)" <cathy.han <at> nsn.com>
Subject: Re: bug#11991: tr bug
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




Changed bug title to 'shell file globbing confusion' from 'tr bug' Request was from Bob Proulx <bob <at> proulx.com> to control <at> debbugs.gnu.org. (Thu, 19 Jul 2012 16:46:04 GMT) Full text and rfc822 format available.

Added tag(s) notabug. Request was from Bob Proulx <bob <at> proulx.com> to control <at> debbugs.gnu.org. (Thu, 19 Jul 2012 16:46:04 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 11991 <at> debbugs.gnu.org and "Xiao, Bellon (NSN - CN/Cheng Du)" <bellon.xiao <at> nsn.com> Request was from Bob Proulx <bob <at> proulx.com> to control <at> debbugs.gnu.org. (Thu, 19 Jul 2012 16:46:05 GMT) Full text and rfc822 format available.

Message #15 received at 11991-done <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: "Xiao, Bellon (NSN - CN/Cheng Du)" <bellon.xiao <at> nsn.com>
Cc: 11991-done <at> debbugs.gnu.org, "Han,
	Cathy \(NSN - CN/Cheng Du\)" <cathy.han <at> nsn.com>
Subject: Re: bug#11991: tr bug
Date: Thu, 19 Jul 2012 10:44:15 -0600
[Message part 1 (text/plain, inline)]
tag 11991 notabug
thanks

[Your message was encoded in Chinese; these days, you are more likely to
achieve interoperability if you encode in UTF-8 instead]

On 07/19/2012 12:38 AM, Xiao, Bellon (NSN - CN/Cheng Du) wrote:
> Hi,
> 
> We found a bug of tr, the version is tr (coreutils) 5.2.1

That's extremely old.  Have you considered updating?  The latest stable
version is 8.17, and there have been fixes to tr in the meantime (but
not for your particular issue).

> Here is the symptom:
> When there is a file named e or r under current directory, tr will take effect,like:
> 	echo hello |tr [a-z] [A-Z] 
> 	echo hello |tr [:lower:] [:upper:]

You are missing shell quoting.

> will return hello
> 
> And when is a file named l, o, p, u or w under current directory, tr will give error message like:
> 	echo hello |tr [a-z] [A-Z] 
> 	echo hello |tr [:lower:] [:upper:]
> gives : tr: misaligned [:upper:] and/or [:lower:] construct

Try the following to see the difference shell quoting makes:

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

The shell is expanding your glob before 'tr' ever sees the command line,
because you happened to have files that matched the glob; in turn, that
means you weren't executing the tr arguments that you thought you were.
 The quoting is necessary to avoid shell globbing from interfering.

I'm therefore closing this as not a bug in coreutils, but in your usage
pattern.

-- 
Eric Blake   eblake <at> redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 17 Aug 2012 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 12 years and 310 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.