How to make bash’s glob character classes case-sensitive

bashglobbing

I had created some files like knob_A.png and knob_a.png and my teammate on Windows said this caused problems with her app. I decided to call it knob_W.png instead of knob_a.png. Then I did an rsync up to our shared server. In order to clean things up I then did

rm knob_[a-d]*.png

and it removed knob_A.png too. This is wrong as a football bat.

Neither shopt -s nocaseglob nor shopt -u nocaseglob causes it to behave the way I want.

How do I tell bash to make its globs be case-sensitive like in the old days?

Best Answer

Bash is being case sensitive here. The problem is with the sorting order of the characters in the range. From the Bash manual (info bash):

The sorting order of characters in range expressions is determined by the current locale and the value of the 'LC_COLLATE' shell variable, if set.

For example, in the default C locale, '[a-dx-z]' is equivalent to '[abcdxyz]'. Many locales sort characters in dictionary order, and in these locales '[a-dx-z]' is typically not equivalent to '[abcdxyz]'; it might be equivalent to '[aBbCcDdxXyYz]', for example. To obtain the traditional interpretation of ranges in bracket expressions, you can force the use of the C locale by setting the 'LC_COLLATE' or 'LC_ALL' environment variable to the value 'C'.

Try doing

export LC_COLLATE=C
Related Question