106

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

user339946
  • 5,961
  • 9
  • 52
  • 97

3 Answers3

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

Community
  • 1
  • 1
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 14
    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. – kojiro Oct 18 '13 at 04:34
  • Yup - all further information available at the doc link above. – Carl Norum Oct 18 '13 at 04:34
  • 2
    bash is weird. `type [[` says that `[[` is a shell keyword, but the manpage does refer to `[[` as a *conditional command* at least once. Meh. – kojiro Oct 18 '13 at 04:36
  • 8
    `[[` is a shell keyword which introduces a compound command `[[ ... ]]`, just like `if` is a shell keyword that introduces the `if..then..fi` compound command. – chepner Oct 18 '13 at 12:45
  • perhaps it is called the Equal Tilde operator – serup Feb 26 '16 at 07:34
  • How can you use matching operator with logical operator? e.g. "if [ [[ $a =~ 'ab' ]] -o [[ $b =~ 'cd' ]] ]; then ... " is working ? – Joonho Park Apr 10 '20 at 09:01
  • @JoonhoPark - I'd recommend against mixing & matching `[` and `[[` like that. Why not just `if [[ $a =~ 'ab' || $b =~ 'cd' ]]`? – Carl Norum Apr 10 '20 at 19:10
  • +1 In addition to a great answer, specifying a link to bash documentation encourages readers to dig in deeper. Thanks! – ZeZNiQ Sep 15 '21 at 15:20
54

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

RubioRic
  • 2,442
  • 4
  • 28
  • 35
Wirawan Purwanto
  • 3,613
  • 3
  • 28
  • 28
  • Thanks for the easy and useful explanation of the difference between `[[ ]]` and `[ ]`... It cleared my mind – LukeSavefrogs Jun 25 '19 at 01:07
  • Though this is focusing on only a part of the question, it is helpful. – 0xc0de Aug 31 '20 at 09:47
  • @LukeSavefrogs what did you understand from this? Can you help me to understand this? – SD. Dec 08 '21 at 10:56
  • @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 – LukeSavefrogs Dec 09 '21 at 12:33
7

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 =~.

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44