title: How can I match a string with a regex in Bash?
body: <p>I am trying to write a bash script that contains a function so when given a <code>.tar</code>, <code>.tar.bz2</code>, <code>.tar.gz</code> etc. file it uses tar with the relevant switches to decompress the file.</p>
  
  <p>I am using if elif then statements which test the filename to see what it ends with and I cannot get it to match using regex metacharacters.</p>
  
  <p>To save constantly rewriting the script I am using 'test' at the command line, I thought the statement below should work, I have tried every combination of brackets, quotes and metacharaters possible and still it fails.</p>
  
  <pre><code>test sed-4.2.2.tar.bz2 = tar\.bz2$; echo $?
  (this returns 1, false)
  </code></pre>
  
  <p>I'm sure the problem is a simple one and I've looked everywhere, yet I cannot fathom how to do it.  Does someone know how I can do this?</p>