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

标签 regex linux bash shell

我遇到了一个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

关于regex - shell脚本中 "=~"运算符的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12454731/

相关文章:

regex - 级联-正则表达式解析器-错​​误的字段数

c# - 在 ITextViewLine 上运行正则表达式(Visual Studio 扩展)

regex - 有没有办法缩短这个正则表达式?

Linux 暂停/恢复操作的用户空间通知

linux - 如何将可移动媒体设为私有(private)

bash - 反转文本文件中的任何行(rev 命令的实现)

regex - Vim 替换 : Delete All between specified words

c - 在C、linux中通过poll fd获取unix socket连接是否需要额外的参数或设置?

linux - 从列表/变量中删除项目 - Bash

python - 用于执行 Python 程序的 Bash 脚本