我一直在尝试 bash 内部大小写转换,发现我认为不一致。
我定义了一个值为“aBcDeF”的变量,并尝试将 [AD] 范围内的第一个大写字符转换为小写。我希望得到“abcDeF”的结果,但相反,我得到:
$ lowerFirst=aBcDeF
$ echo ${lowerFirst,[A-D]}
aBcDeF
它似乎与小写“a”匹配,看到它已经是小写并得出结论,无事可做。
bash手册页说
${parameter,pattern}
该模式被扩展以产生一个模式,就像在文件名扩展中一样。参数扩展值中的每个字符都针对模式进行测试,如果匹配模式,则转换其大小写。模式不应尝试匹配多个字符。
这使我研究了关于文件名扩展的部分,这似乎很清楚第一个大写字符应该匹配。
这让我想知道大小写转换,一般来说,事情不像手册页所示的那样工作。
我的脚本:
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)
我是否正确解释了这一点?如果没有,我做错了什么?