regex - shell脚本中 "=~"运算符的含义

这个问题在这里已经有了答案:
关闭6年前

我遇到了一个shell脚本,代码是

for line in $LIST_ARRAY;do
if [[ $LIST_ARRAY =~ $line ]]
then
echo "true"
....
...
.

这种情况下=~有什么用?

最佳答案

这是允许在 if 语句中使用正则表达式的 Equal Tilde 运算符。

An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)). The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression's return value is 2. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. Any part of the pattern may be quoted to force it to be matched as a string.

http://linux.die.net/man/1/bash

https://stackoverflow.com/questions/12454731/

相关文章:

linux - Linux 中的直接内存访问

c++ - 如何在 GDB 中漂亮地打印 STL 容器?

linux - 用户数据脚本未在我的自定义 AMI 上运行,但在标准 Amazon linux 中运

linux - 如何删除 Mercurial 存储库

linux - 如何获得linux中两个文件之间的差异(仅添加)

linux - 自动化 Amazon EBS 快照 任何人在 linux 上都有一个好的脚本或解决方

linux - 如何使用 sed 提取子字符串

php - 在 PHP/Apache/Linux 环境中,为什么 chmod 777 是危险的?

linux - 在 mac 上通过 ssh 连接到 amazon aws linux 服务器

python - 编译 python 时 --enable-optimizations 做了什么?