111

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

0

3 Answers 3

111
  1. What is the operator =~ called?

    I'm not sure it has a name. The bash documentation just calls it the =~ operator.

  2. 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.

  3. Why are double square brackets required when running a test?

    Because =~ is an operator of the [[ expression ]] compound command.

Sign up to request clarification or add additional context in comments.

8 Comments

If the left side matches, the operator returns 0 or 1 as you say, but it also sets the BASH_REMATCH array, which bears mentioning. Also, [[ is not a command – It's a keyword.
Yup - all further information available at the doc link above.
bash is weird. type [[ says that [[ is a shell keyword, but the manpage does refer to [[ as a conditional command at least once. Meh.
[[ is a shell keyword which introduces a compound command [[ ... ]], just like if is a shell keyword that introduces the if..then..fi compound command.
perhaps it is called the Equal Tilde operator
|
58

The =~ operator is a regular expression match operator. This operator is inspired by Perl's use of the same operator for regular expression matching.

The [[ ]] is treated specially by bash; consider that an augmented version of [ ] construct:

  • [ ] is actually a shell built-in command, which, can actually be implemented as an external command. Look at your /usr/bin, there is most likely a program called "[" there! Strictly speaking, [ ] is not part of bash syntax.

  • [[ ]] is a shell keyword, which means it is part of shell syntax. Inside this construct, some reserved characters change meaning. For example, ( ) means parenthesis like other programming language (not launching a subshell to execute what's inside the parentheses). Another example is that < and > means less than and greater than, not shell redirection. This allow more "natural" appearance of logical expressions, but it can be confusing for novice bash programmers.

Wirawan

4 Comments

Thanks for the easy and useful explanation of the difference between [[ ]] and [ ]... It cleared my mind
Though this is focusing on only a part of the question, it is helpful.
@LukeSavefrogs what did you understand from this? Can you help me to understand this?
@SD. [ and [[ are two different commands. Sometimes they are interchangeable but as of now it is always better to use the latter, which is more secure
8

The =~operator is the pattern match operator. It did not exist in the original Bourne shell, when test, or internally [ ], was used for conditionals.

The let command, or [[ ]] internally, has more functionality than test, including pattern matching capabilities. This is why you have to use [[ ]], instead of [ ], when using =~.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.