Meaning of "=~" operator in shell script

68,402

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

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

Share:
68,402
cc4re
Author by

cc4re

Cc4rE [A polyglot in programming] Software Engineer , Non-Artistic developer , currently working as a Member of the Technical Team at an International Organization . I love coding ,as a programmer . My interest is to contribute more to the Open source development , I use Linux and Windows for my development.I works frequently on Java , js , GCC and Dot-net , as an Independent developer i have worked on many languages , Created applications on RAD and Low level driver interfacing . You can connect me anytime for any doubts and queries related to Programming.

Updated on August 07, 2020

Comments

  • cc4re
    cc4re almost 4 years

    I came across a shell script where the code is

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

    What is the use of =~ in this case?