Shell – Strange behaviour of tr using ranges

command lineshelltrwildcards

I have one particular server that is exhibiting strange behaviour when using tr. Here is an example from a working server:

-bash-3.2$ echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-z]
1234567890
-bash-3.2$

That makes perfect sense to me.

This, however, is from the 'special' server:

[root@host~]# echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-z]
abcdefghijklmnpqrstuvwxyz1234567890

As you can see, deleting all lower case characters fails. BUT, it has deleted the letter 'o'

The interesting part is the following two examples, which make no sense to me whatsoever:

[root@host~]# echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-n]
opqrstuvwxyz1234567890
[root@host~]# echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-o]
abcdefghijklmnpqrstuvwxyz1234567890
[root@host~]#

(again, the 'o' is deleted in the last example)

Does anyone have any idea what is going on here? I can't reproduce on any other linux box that I am using.

Best Answer

you have a file named o in current directory

foo> ls
foo> echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-z]
1234567890
foo> touch o
foo> echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-z]
abcdefghijklmnpqrstuvwxyz1234567890

shell will expand [a-z] string if a match is found.

This is called pathname expansion, according to man bash

Pathname Expansion
After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. ... (...)

bash will perform expansion.

[...] Matches any one of the enclosed characters.

Related Question