What is the operator =~
called? Is it only used to compare the right side against the left side?
Why are double square brackets required when running a test?
ie. [[ $phrase =~ $keyword ]]
Thank you
A regular expression matching sign, the =~ operator, is used to identify regular expressions.
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)).
Regex is a very powerful tool that is available at our disposal & the best thing about using regex is that they can be used in almost every computer language. So if you are Bash Scripting or creating a Python program, we can use regex or we can also write a single line search query.
The [[ ... ]] part allows to test a condition using operators. Think of it as an if statement. In your example, you're using the -s operator, which tests that the referenced file is not empty.
What is the operator
=~
called?
I'm not sure it has a name. The bash documentation just calls it the =~
operator.
Is it only used to compare the right side against the left side?
The right side is considered an extended regular expression. If the left side matches, the operator returns 0
, and 1
otherwise.
Why are double square brackets required when running a test?
Because =~
is an operator of the [[ expression ]]
compound command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With