What is up with [A-Z] meaning [A-Za-z]?

后端 未结 3 593
暗喜
暗喜 2021-02-07 22:44

I\'ve noticed for a while now that, on some of the Unix-based systems I use at least, ls [A-Z]* has been giving me the results I would anticipate from ls [A-Z

相关标签:
3条回答
  • 2021-02-07 23:24

    It's actually [A-Za-y], and it has to do with language collation. If you want to override it then set $LC_COLLATE appropriately; either of C or POSIX should do.

    0 讨论(0)
  • 2021-02-07 23:37

    Unix shells don't actually use regular expressions, but glob patterns, which are distinctly different from regexes. One difference is that they are implicitly anchored to the start and end of the string - e.g. ls foo[a-z] will list the file food, but not fooble. It's not actually ls doing the matching here, but the shell itself. Globs are also usually sometimes case insensitive (depending on the implementation).

    Take a look at the manpage for your favourite interactive shell and read about glob matching - for example bash's manpage about filename expansion describes the syntax it uses.

    0 讨论(0)
  • 2021-02-07 23:38

    It depends on your locale. If you want that [A..Z] only matches capital letters, you could use C locale: set LC_COLLATE or LC_ALL to C.

    LC_ALL=C
    ls [A..Z]*
    

    bash manual, pattern matching

    0 讨论(0)
提交回复
热议问题