Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a string and a hash returned by openssl, why using == doesn't match them correctly but =~ does?

Background question: How can I force a script to use more resources?

share|improve this question
up vote 8 down vote accepted

The binary operator, ‘=~’, has 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 regex3)). 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.

From: bash

Hence your comparing for equality versus for a regular expression match.

share|improve this answer
4  
(and beware that means substring match. The b regular expression is matched in abc, so [[ abc =~ b ]] is true, while for the fnmatch pattern matching operator ([[ abc == b ]]), it's not. The equivalent of [[ abc =~ b ]] is [[ abc == *b* ]]). – Stéphane Chazelas Mar 13 '14 at 11:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.