title: Using Bash regex match to test membership
body: <p>Below, I use ALLOWED as container to test a token. 
  I am using a Bash regex match syntax <code>=~</code> where the right hand side should be an extended regular expression.</p>
  
  <p>In Bash's Regular Expression Matching. Using the operator =~, the left hand side operand is matched against the extended regular expression (ERE) on the right hand side. Check a related question on using <a href="https://stackoverflow.com/questions/21112707/check-if-a-string-matches-a-regex-in-bash-script">date regex</a>.</p>
  
  <p>But I can't see <code>str1</code> as a regex and I don't know why <code>ALLOWED</code> matches a string which is present inside it.  Even as this works in this case, having regex (str1) as the test string leaves it open for tricky bugs in future.</p>
  
  <pre><code>export ALLOWED="str0 str1 strn"
  export STR1="str1"
  export STR2="str2"
  
  if [[ $ALLOWED =~ ${STR1} ]]; then
      echo "how does it this work?"
  fi
  
  if [[ $ALLOWED =~ ${STR2} ]]; then
      :
  else
      echo "does not work."
  fi
  </code></pre>
  
  <p>Questions:</p>
  
  <ul>
  <li>Why/ How does this work?</li>
  <li>What's a better to do test for an element in a list in bash?</li>
  </ul>