我以为我理解了可选 ?(pattern-list)
in bash
(当extglob
shell 选项打开时)和默认情况下 in 的使用ksh
。例如在bash
:
$ shopt -s extglob
$ V=35xAB
$ echo "${V#?(35|88)x}" "${V#35}"
AB xAB
但是当匹配的前缀模式只是 one?()
或 one*()
时,引入了我所说的可选模式,35
除非##
使用,否则不会省略:
$ echo "${V#?(35|88)}" "${V#*(35|88)}" # Why 35 is not left out?
35xA 35xA
$ echo "${V##?(35|88)}" "${V##*(35|88)}" # Why is it omitted when ## is used?
xA xA
当?()
和*()
用于匹配的后缀模式(使用%
and %%
)时,会报告相同的行为:
$ echo "${V%5?(xA|Bz)}" # 5xA is omitted
3
$ echo "${V%?(xA|Bz)}" "${V%*(xA|Bz)}" # why xA is not left out?
35xA 35xA
$ echo "${V%%?(xA|Bz)}" "${V%%*(xA|Bz)}" # xA is omitted when %% is used
35 35
bash
我在3.2.25、4.1.2 和 4.1.6 版本中测试了这个问题,这让我觉得,也许我没有正确理解匹配模式的实际底层 shell 机制。
有人可以解释一下吗?
提前致谢