gpt4 book ai didi

Bash ,, 和 ^^ 大小写转换不匹配模式

转载 作者:行者123 更新时间:2023-11-29 09:29:13 30 4
gpt4 key购买 nike

我一直在试验 bash 内部大小写转换,发现了我认为不一致的地方。

我定义了一个值为“aBcDeF”的变量,并尝试将范围 [A-D] 中的第一个大写字符转换为小写。我希望得到“abcDeF”的结果,但相反,我得到:

$ lowerFirst=aBcDeF
$ echo ${lowerFirst,[A-D]}
aBcDeF

它似乎与小写字母“a”匹配,看到它已经是小写字母并断定没有什么可做的。

bash man page

${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 ,, 和 ^^ 大小写转换不匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54729609/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com