title: BASH Expression to replace beginning and ending of a string in one operation?
body: <p>Here's a simple problem that's been bugging me for some time. I often find I have a number of input files in some directory, and I want to construct output file names by replacing beginning and ending portions. For example, given this:</p>
  
  <pre><code>source/foo.c
  source/bar.c
  source/foo_bar.c
  </code></pre>
  
  <p>I often end up writing BASH expressions like:</p>
  
  <pre><code>for f in source/*.c; do
    a="obj/${f##*/}"
    b="${a%.*}.obj"
    process "$f" "$b"
  done
  </code></pre>
  
  <p>to generate the commands</p>
  
  <pre><code>process "source/foo.c"    "obj/foo.obj"
  process "source/bar.c     "obj/bar.obj"
  process "source/foo_bar.c "obj/foo_bar.obj"
  </code></pre>
  
  <p>The above works, but its a lot wordier than I like, and I would prefer to avoid the temporary variables. Ideally there would be some command that could replace the beginning and ends of a string in one shot, so that I could just write something like:</p>
  
  <pre><code>for f in source/*.c; do process "$f" "obj/${f##*/%.*}.obj"; done
  </code></pre>
  
  <p>Of course, the above doesn't work. Does anyone know something that will? I'm just trying to save myself some typing here.</p>