title: Capturing Groups From a Grep RegEx
body: <p>I've got this little script in <code>sh</code> (Mac OSX 10.6) to look through an array of files. Google has stopped being helpful at this point:</p>
  
  <pre><code>files="*.jpg"
  for f in $files
      do
          echo $f | grep -oEi '[0-9]+_([a-z]+)_[0-9a-z]*'
          name=$?
          echo $name
      done
  </code></pre>
  
  <p>So far (obviously, to you shell gurus) <code>$name</code> merely holds 0, 1 or 2, depending on if <code>grep</code> found that the filename matched the matter provided. <strong>What I'd like is to capture what's inside the parens <code>([a-z]+)</code> and store that to a variable</strong>.</p>
  
  <p>I'd like <strong>to use <code>grep</code> only, if possible</strong>. If not, please no Python or Perl, etc. <code>sed</code> or something like it – I'm new to shell and would like to attack this from the *nix purist angle.</p>
  
  <p>Also, as a <strong>super-cool bonu</strong>s, I'm curious as to how I can concatenate string in shell? Is the group I captured was the string "somename" stored in $name, and I wanted to add the string ".jpg" to the end of it, could I <code>cat $name '.jpg'</code>?</p>
  
  <p>Please explain what's going on, if you've got the time.</p>