Bash , 和 ^^ 大小写转换不匹配模式
Bash ,, and ^^ case conversion are not matching patterns
我一直在试验 bash 内部大小写转换,发现我认为不一致。
我定义了一个值为 "aBcDeF" 的变量,并尝试将范围 [A-D] 中的第一个大写字符转换为小写。我希望得到 "abcDeF" 的结果,但我得到的是:
$ lowerFirst=aBcDeF
$ echo ${lowerFirst,[A-D]}
aBcDeF
它似乎匹配小写字母 "a",看到它已经是小写字母并断定没有什么可做的。
${parameter,pattern}
The pattern is expanded to produce a pattern just as in filename
expansion. Each character in the expanded value of parameter is tested
against pattern, and, if it matches the pattern, its case is
converted. The pattern should not attempt to match more than one character.
这让我研究了关于 filename expansion 的部分,它似乎很清楚第一个大写字符应该匹配。
这让我对大小写转换感到疑惑,一般来说事情并不像手册页所指示的那样工作。
我的脚本:
lowerFirst=aBcDeF
upperFirst=AbCdEf
echo 'lowerFirst=aBcDeF'
echo 'upperFirst=AbCdEf'
echo
echo 'Original Converted Expected Conversion'
echo "${lowerFirst} ${lowerFirst,[A-D]} abcDeF "'${lowerFirst,[A-D]}'
echo "${upperFirst} ${upperFirst,[A-D]} abCdEf "'${upperFirst,[A-D]}'
echo "${lowerFirst} ${lowerFirst,[a-d]} aBcDeF "'${lowerFirst,[a-d]}'
echo "${upperFirst} ${upperFirst,[a-d]} AbCdEf "'${upperFirst,[a-d]}'
echo "${lowerFirst} ${lowerFirst^[A-D]} aBcDeF "'${lowerFirst^[A-D]}'
echo "${upperFirst} ${upperFirst^[A-D]} AbCdEf "'${upperFirst^[A-D]}'
echo "${lowerFirst} ${lowerFirst^[a-d]} abcDeF "'${lowerFirst^[a-d]}'
echo "${upperFirst} ${upperFirst^[a-d]} abCdEf "'${upperFirst^[a-d]}'
echo
echo "${lowerFirst} ${lowerFirst,,[A-D]} abcdeF "'${lowerFirst,,[A-D]}'
echo "${upperFirst} ${upperFirst,,[A-D]} abcdEf "'${upperFirst,,[A-D]}'
echo "${lowerFirst} ${lowerFirst,,[a-d]} aBcDeF "'${lowerFirst,,[a-d]}'
echo "${upperFirst} ${upperFirst,,[a-d]} AbCdEf "'${upperFirst,,[a-d]}'
echo "${lowerFirst} ${lowerFirst^^[A-D]} aBcDeF "'${lowerFirst^^[A-D]}'
echo "${upperFirst} ${upperFirst^^[A-D]} AbCdEf "'${upperFirst^^[A-D]}'
echo "${lowerFirst} ${lowerFirst^^[a-d]} ABCDeF "'${lowerFirst^^[a-d]}'
echo "${upperFirst} ${upperFirst^^[a-d]} ABCDEf "'${upperFirst^^[a-d]}'
这给出:
lowerFirst=aBcDeF
upperFirst=AbCdEf
Original Converted Expected Conversion
aBcDeF aBcDeF abcDeF ${lowerFirst,[A-D]}
AbCdEf abCdEf abCdEf ${upperFirst,[A-D]}
aBcDeF aBcDeF aBcDeF ${lowerFirst,[a-d]}
AbCdEf AbCdEf AbCdEf ${upperFirst,[a-d]}
aBcDeF aBcDeF aBcDeF ${lowerFirst^[A-D]}
AbCdEf AbCdEf AbCdEf ${upperFirst^[A-D]}
aBcDeF ABcDeF abcDeF ${lowerFirst^[a-d]}
AbCdEf AbCdEf abCdEf ${upperFirst^[a-d]}
aBcDeF abcdeF abcdeF ${lowerFirst,,[A-D]}
AbCdEf abcdEf abcdEf ${upperFirst,,[A-D]}
aBcDeF aBcDeF aBcDeF ${lowerFirst,,[a-d]}
AbCdEf AbCdEf AbCdEf ${upperFirst,,[a-d]}
aBcDeF aBcDeF aBcDeF ${lowerFirst^^[A-D]}
AbCdEf AbCdEf AbCdEf ${upperFirst^^[A-D]}
aBcDeF ABCDeF ABCDeF ${lowerFirst^^[a-d]}
AbCdEf ABCDEf ABCDEf ${upperFirst^^[a-d]}
如果我要求 bash 版本,我得到:
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
我的解释正确吗?如果不是,我哪里错了?
bash 手册指出:
The ^^
and ,,
expansions convert each matched character in the expanded value; the ^
and ,
expansions match and convert only the first character in the expanded value.
,
如果第一个字符与模式匹配,则将其转换为小写。 ,,
将转换所有匹配的字符。无法只转换第一个匹配字符。
我一直在试验 bash 内部大小写转换,发现我认为不一致。
我定义了一个值为 "aBcDeF" 的变量,并尝试将范围 [A-D] 中的第一个大写字符转换为小写。我希望得到 "abcDeF" 的结果,但我得到的是:
$ lowerFirst=aBcDeF
$ echo ${lowerFirst,[A-D]}
aBcDeF
它似乎匹配小写字母 "a",看到它已经是小写字母并断定没有什么可做的。
${parameter,pattern}
The pattern is expanded to produce a pattern just as in filename expansion. Each character in the expanded value of parameter is tested against pattern, and, if it matches the pattern, its case is converted. The pattern should not attempt to match more than one character.
这让我研究了关于 filename expansion 的部分,它似乎很清楚第一个大写字符应该匹配。
这让我对大小写转换感到疑惑,一般来说事情并不像手册页所指示的那样工作。
我的脚本:
lowerFirst=aBcDeF
upperFirst=AbCdEf
echo 'lowerFirst=aBcDeF'
echo 'upperFirst=AbCdEf'
echo
echo 'Original Converted Expected Conversion'
echo "${lowerFirst} ${lowerFirst,[A-D]} abcDeF "'${lowerFirst,[A-D]}'
echo "${upperFirst} ${upperFirst,[A-D]} abCdEf "'${upperFirst,[A-D]}'
echo "${lowerFirst} ${lowerFirst,[a-d]} aBcDeF "'${lowerFirst,[a-d]}'
echo "${upperFirst} ${upperFirst,[a-d]} AbCdEf "'${upperFirst,[a-d]}'
echo "${lowerFirst} ${lowerFirst^[A-D]} aBcDeF "'${lowerFirst^[A-D]}'
echo "${upperFirst} ${upperFirst^[A-D]} AbCdEf "'${upperFirst^[A-D]}'
echo "${lowerFirst} ${lowerFirst^[a-d]} abcDeF "'${lowerFirst^[a-d]}'
echo "${upperFirst} ${upperFirst^[a-d]} abCdEf "'${upperFirst^[a-d]}'
echo
echo "${lowerFirst} ${lowerFirst,,[A-D]} abcdeF "'${lowerFirst,,[A-D]}'
echo "${upperFirst} ${upperFirst,,[A-D]} abcdEf "'${upperFirst,,[A-D]}'
echo "${lowerFirst} ${lowerFirst,,[a-d]} aBcDeF "'${lowerFirst,,[a-d]}'
echo "${upperFirst} ${upperFirst,,[a-d]} AbCdEf "'${upperFirst,,[a-d]}'
echo "${lowerFirst} ${lowerFirst^^[A-D]} aBcDeF "'${lowerFirst^^[A-D]}'
echo "${upperFirst} ${upperFirst^^[A-D]} AbCdEf "'${upperFirst^^[A-D]}'
echo "${lowerFirst} ${lowerFirst^^[a-d]} ABCDeF "'${lowerFirst^^[a-d]}'
echo "${upperFirst} ${upperFirst^^[a-d]} ABCDEf "'${upperFirst^^[a-d]}'
这给出:
lowerFirst=aBcDeF
upperFirst=AbCdEf
Original Converted Expected Conversion
aBcDeF aBcDeF abcDeF ${lowerFirst,[A-D]}
AbCdEf abCdEf abCdEf ${upperFirst,[A-D]}
aBcDeF aBcDeF aBcDeF ${lowerFirst,[a-d]}
AbCdEf AbCdEf AbCdEf ${upperFirst,[a-d]}
aBcDeF aBcDeF aBcDeF ${lowerFirst^[A-D]}
AbCdEf AbCdEf AbCdEf ${upperFirst^[A-D]}
aBcDeF ABcDeF abcDeF ${lowerFirst^[a-d]}
AbCdEf AbCdEf abCdEf ${upperFirst^[a-d]}
aBcDeF abcdeF abcdeF ${lowerFirst,,[A-D]}
AbCdEf abcdEf abcdEf ${upperFirst,,[A-D]}
aBcDeF aBcDeF aBcDeF ${lowerFirst,,[a-d]}
AbCdEf AbCdEf AbCdEf ${upperFirst,,[a-d]}
aBcDeF aBcDeF aBcDeF ${lowerFirst^^[A-D]}
AbCdEf AbCdEf AbCdEf ${upperFirst^^[A-D]}
aBcDeF ABCDeF ABCDeF ${lowerFirst^^[a-d]}
AbCdEf ABCDEf ABCDEf ${upperFirst^^[a-d]}
如果我要求 bash 版本,我得到:
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
我的解释正确吗?如果不是,我哪里错了?
bash 手册指出:
The
^^
and,,
expansions convert each matched character in the expanded value; the^
and,
expansions match and convert only the first character in the expanded value.
,
如果第一个字符与模式匹配,则将其转换为小写。 ,,
将转换所有匹配的字符。无法只转换第一个匹配字符。