I've got this script in `sh` (macOS 10.6) to look through an array of files: files="*.jpg" for f in $files do echo $f | grep -oEi '[0-9]+_([a-z]+)_[0-9a-z]*' name=$? echo $name done So far `$name` merely holds 0, 1 or 2, depending on if `grep` found that the filename matched the matter provided. What I'd like is to capture what's inside the parens `([a-z]+)` and store that to a variable. I'd like to use `grep` only, if possible. If not, please no Python or Perl, etc. `sed` or something like it – I would like to attack this from the \*nix purist angle.