gpt4 book ai didi

bash - 为什么使用通配符时 Bash 条件不对称?

转载 作者:行者123 更新时间:2023-12-03 15:34:56 25 4
gpt4 key购买 nike

假设我有以下 Bash 片段:

if [[ "$foo" == *"$bar"* ]] ; then
echo 'condition is true'
fi

在英语中,你可以用以下代码来描述上面的代码:if barfoo 的子串然后...

但是,为什么当我们切换条件的两边时,我们没有得到相同的结果?

if [[ *"$bar"* == "$foo" ]] ; then
echo 'condition is true'
fi

也许我对通配符的评估时间有误解?

最佳答案

这是一个明确的设计决策。这是man bash :

When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described


[[ .. ]] 中未执行路径名扩展,所以围绕它的常规规则不适用。

将两者都视为一种模式会很奇怪。应 [[ a* == [a]* ]]正确是因为正确的模式匹配左字符串,错误是因为左模式不匹配正确的字符串,还是正确是因为两个模式匹配相同的字符串集?

关于bash - 为什么使用通配符时 Bash 条件不对称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62096320/

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