title: How do I manipulate $PATH elements in shell scripts?
body: <p>Is there a idiomatic way of removing elements from PATH-like shell variables?</p>
  
  <p>That is I want to take</p>
  
  <pre><code>PATH=/home/joe/bin:/usr/local/bin:/usr/bin:/bin:/path/to/app/bin:.
  </code></pre>
  
  <p>and <strong>remove</strong> or <strong>replace</strong> the <code>/path/to/app/bin</code> without clobbering the rest of the variable. Extra points for allowing me <em>put</em> new elements in arbitrary positions. The target will be recognizable by a well defined string, and may occur at any point in the list.</p>
  
  <p>I know I've seen this done, and can probably cobble something together on my own, but I'm looking for a nice approach. Portability and standardization a plus.</p>
  
  <p>I use bash, but example are welcome in your favorite shell as well.</p>
  
  <hr>
  
  <p>The context here is one of needing to switch conveniently between multiple versions (one for doing analysis, another for working on the framework) of a large scientific analysis package which produces a couple dozen executables, has data stashed around the filesystem, and uses environment variable to help find all this stuff. I would like to write a script that selects a version, and need to be able to remove the <code>$PATH</code> elements relating to the currently active version and replace them with the same elements relating to the new version.</p>
  
  <hr>
  
  <p>This is related to the problem of preventing repeated <code>$PATH</code> elements when re-running login scripts and the like.</p>
  
  <hr>
  
  <ul>
  <li>Previous similar question: <a href="https://stackoverflow.com/questions/135754/how-to-keep-from-duplicating-path-variable-in-csh">How to keep from duplicating path variable in csh</a></li>
  <li>Subsequent similar question: <a href="https://stackoverflow.com/questions/370047/what-is-the-most-elegant-way-to-remove-a-path-from-the-path-variable-in-bash">What is the most elegant way to remove a path from the $PATH variable in Bash?</a></li>
  </ul>