Bash - 检查变量是否与许多选项中的任何一个不同

时间:2013-04-23 03:12:48

标签: bash if-statement

如果我有代码

if [[ this != that && this != another ]]

有没有办法让它分拣?像

这样的东西
if [[ this !=that && != another ]]

当然,这样做不会奏效,但可以缩短条件。

1 个答案:

答案 0 :(得分:4)

使用bash,您可以在[[

中使用正则表达式
if [[ ! this =~ ^(that|another|this\ one)$ ]]; then
   # do something
fi

如果您习惯使用任何其他编程语言,!(非)运算符的优先级可能会令人困惑。另外,请注意:不要将正则表达式放在引号中。如果你这样做,它就不再是正则表达式了。