Different behavour with "tr"

I am getting different output with the 'tr' command in bash
I am using
Linux:
Red Hat Enterprise Linux Server release 5.3 (Tikanga)
Bash version:
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)

$ x=`echo XXx | tr '[a-z]' '[A-Z]' `
$ echo $x
XXX

$ y=`echo XXx | tr [:lower:] [:upper:] `
$ echo $y
XXx

Probably a character-set thing. The GNU utilities try to abide by your character set whenever possible, and UTF8 is increasingly standard, but the definition of :lower: and :upper: for UTF8 is going to be pretty big and complicated.

Other side-effects of this compliance are things like slow-performing grep -i.

Try LOCALE="C" tr ... and see if it works as expected.

Also, if you have a file in your directory named :, l, o, w, e, r, u, or p, [:lower:] and [:upper:] will actually glob those filenames, so put them in single quotes! '[:lower:]' '[:upper:]'

1 Like

I believe the globbing is the problem. If [:lower:] matches a filename, the lower case x won't appear in the first argument to tr and will not be converted.

The issue with locales which interleave upper and lower case letters (such as english utf-8) typically manifests as unintended conversions.

I would suggest, as you did, to quote the arguments to tr. For confirmation, set -x .

Regards,
Alister

1 Like

that explains it.
globbing was the culprit in this case. I had a file with 'w' in directory where I was executing this command. Thanks for your help !