shell腳本中的“=~”運算符的含義[重復]

[英]Meaning of “=~” operator in shell script [duplicate]


This question already has an answer here:

這個問題已經有了答案:

I came across a shell script where the code is

我遇到了代碼所在的shell腳本

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

What is the use of =~ in this case?

在這種情況下=~有什么用?

1 个解决方案

#1


46  

it's the Equal Tilde operator that allows the use of regex in an if statement.

它是等傾算子,允許在if語句中使用regex。

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.

另一個二進制運算符=~可用,其優先級與==和!=相同。當它被使用時,運算符右邊的字符串被視為一個擴展正則表達式,並相應地進行匹配(如regex(3)))。如果字符串匹配模式,則返回值為0,否則返回值為1。如果正則表達式語法不正確,條件表達式的返回值為2。如果啟用了shell選項nocasematch,則不考慮字母字符的情況而執行匹配。模式的任何部分都可以被引用,以迫使它被匹配為一個字符串。

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

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


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/09/17/7257c245e06db41cf63ce46c96ce5c87.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com