当关闭nocasematch时,为什么case语句不区分大小写? [英] Why isn't the case statement case-sensitive when nocasematch is off?

查看:388
本文介绍了当关闭nocasematch时,为什么case语句不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下内容:

$ echo $BASH_VERSION
4.2.10(1)-release

$ shopt | fgrep case
nocaseglob      off
nocasematch     off

$ case A in [a-z]) echo TRUE;; esac
TRUE

我希望大写字母 A 应该 [a-z] 的小写字符类匹配,但是可以.为什么这场比赛不会失败?

I expect that the capital letter A should not match the lower-case character class of [a-z], but it does. Why doesn't this match fail?

推荐答案

您不能以这种方式可靠地使用破折号.如果我不使用破折号,它将按预期工作:

You can't reliably use the dash this way. If I don't use dashes, it works as expected:

$ bash --version
GNU bash, version 4.2.10(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ shopt -s nocasematch
$ case A in [abc]) echo TRUE;; esac
TRUE
$ shopt -u nocasematch
$ case A in [abc]) echo TRUE;; esac
$ 

但是带有破折号的,无论nocasematch的设置如何,它都会打印TRUE.

But with dashes, it prints TRUE regardless of the setting of nocasematch.

Bash在这里进行模式匹配.查看参考手册的本部分,其中说使用连字符MIGHT将[a-z]解释为[A-Za-z]!它告诉您如何获取传统解释(将LC_COLLATE或LC_ALL设置为C).基本上,您的默认语言环境是按字典顺序排序的.参考手册很好地解释了这些情况.

Bash is doing pattern matching here. Check out this section of the reference manual where it says that using the hyphen MIGHT interpret [a-z] as [A-Za-z]! It tells you how to get the traditional interpretation (set LC_COLLATE or LC_ALL to C). Basically your default locale is sorting in dictionary order. The reference manual explains things pretty well.

附录

好的,我有你的成绩单.

Okay I have a transcript for you.

$ shopt -u nocasematch
$ case A in [a-z]) echo TRUE;; esac
TRUE
$ shopt -s nocasematch
$ case A in [a-z]) echo TRUE;; esac
TRUE
$ LC_ALL=C
$ shopt -u nocasematch
$ case A in [a-z]) echo TRUE;; esac
$ shopt -s nocasematch
$ case A in [a-z]) echo TRUE;; esac
TRUE

这篇关于当关闭nocasematch时,为什么case语句不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆