title: Check if a string contains valid pattern in Bash
body: <p>I have a file a.txt contains a string like:
  Axxx-Bxxxx
  Rules for checking if it is valid or not include:</p>
  <ul>
  <li>length is 10 characters.</li>
  <li>x here is digits only.</li>
  </ul>
  <p>Then, I try to check with:</p>
  <pre><code>#!/bin/bash
  exp_len=10;
  file=a.txt;
  msg=&quot;checking string&quot;;
  tmp=&quot;File not exist&quot;;
  
  echo $msg;
  if[ -f $file];then
      tmp=$(cat $file);
      if[[${#tmp} != $exp_len ]];then
          msg=&quot;invalid length&quot;;
      elif [[ $tmp =~ ^[A[0-9]{3}-B[0-9]{4}]$]];then
          msg=&quot;valid&quot;;
      else
          msg=&quot;invalid&quot;;
      fi
  else
      msg=&quot;file not exist&quot;;
  fi
  echo $msg;
  </code></pre>
  <p>But in valid case it doesn't work...
  Is there someone help to correct me?
  Thanks :)</p>