Ubuntu – ‘echo [A-Z]*’ matches lower-case filenames as well

bashfilename

I am working on the root directory and want to print directory entries starting with an upper-case letter.

$ echo [A-Z]*
Applications Library Network System Users Volumes bin cores dev etc home installer.failurerequests net private sbin tmp usr var vm

The result confuses me because I wasn't expecting
cores dev etc home installer.failurerequests net private sbin tmp usr var vm to match my pattern.

How can I write a glob pattern that only matches upper-case letters?

Best Answer

That's because the glob pattern [A-Z] does not generally correspond to uppercase letters. Specifically it expands according to

the current locale's collating sequence and character set

If you want files starting with an upper case letter, you can use

echo [[:upper:]]*

or set the locale explicitly

(LC_COLLATE=C; echo [A-Z]*)

or use the bash globasciiranges shell option

(shopt -s globasciiranges; echo [A-Z]*)

See the Pattern matching section of man bash

Related Question