Remove some path from the PATH variable temporarily.

2010-09-04 403 words 2 mins read

How many times has it happened to you that you are working on some linux platform (like Fedora/Ubuntu/CentOS etc) and suddenly you see that you need to remove some path from the PATH variable so that a script is executed from some other path. It really difficult to do this if the path is too long and if you end up doing this couple of times. If that is the case, then the below script is for you 🙂

  <td>
    <div class="bash codecolorer">
      <span class="co0">#!/bin/bash -</span><br /> <span class="co0">#===============================================================================</span><br /> <span class="co0">#</span><br /> <span class="co0">#          FILE:  remove_path.sh</span><br /> <span class="co0">#</span><br /> <span class="co0">#         USAGE:  ./remove_path.sh</span><br /> <span class="co0">#</span><br /> <span class="co0">#   DESCRIPTION:  Add a path from the $PATH variable</span><br /> <span class="co0">#</span><br /> <span class="co0">#       OPTIONS:  ---</span><br /> <span class="co0">#  REQUIREMENTS:  ---</span><br /> <span class="co0">#          BUGS:  ---</span><br /> <span class="co0">#         NOTES:  ---</span><br /> <span class="co0">#        AUTHOR:  <a class="zem_slink" title="Amit Agarwal" rel="me" href="http://amit-agarwal.co.in">Amit Agarwal</a> (AKA), amit.agarwal@amit-agarwal.co.in</span><br /> <span class="co0">#       COMPANY:  Individual</span><br /> <span class="co0">#       VERSION:  1.0</span><br /> <span class="co0">#       CREATED:  08/08/2010 02:26:16 PM IST</span><br /> <span class="co0">#      REVISION:  ---</span><br /> <span class="co0">#===============================================================================</span><br /> <span class="kw1">if</span> <span class="br0">&#91;</span><span class="br0">&#91;</span> <span class="re4">$#</span> <span class="sy0"><</span> <span class="nu0">1</span> <span class="br0">&#93;</span><span class="br0">&#93;</span><br /> <span class="kw1">then</span><br /> <span class="kw3">echo</span> <span class="st0">"Not enough arguments"</span><br /> <span class="kw3">exit</span> <span class="re5">-999</span><br /> <span class="kw1">fi</span><br /> <span class="kw3">unset</span> PATH_ PATH1<br /> <span class="kw3">export</span> <span class="re2">PATH_</span>=<span class="br0">&#40;</span>$<span class="br0">&#40;</span><span class="kw3">echo</span> <span class="re1">$PATH</span><span class="sy0">|</span><span class="kw2">sed</span> <span class="co3">\'</span>s<span class="sy0">/</span>:<span class="sy0">/</span> <span class="sy0">/</span>g<span class="co3">\'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br /> <span class="kw1">for</span> i <span class="kw1">in</span> <span class="sy0">`</span><span class="kw2">seq</span> <span class="nu0">1</span> <span class="co1">${#PATH_}</span><span class="sy0">`</span><br /> <span class="kw1">do</span><br /> <span class="kw3">echo</span> <span class="co1">${PATH_[$i]}</span><br /> <span class="kw3">echo</span> <span class="co1">${PATH_[$i]}</span><span class="sy0">|<</span>a <span class="re2">class</span>=<span class="st0">"zem_slink"</span> <span class="re2">title</span>=<span class="st0">"Grep"</span> <span class="re2">rel</span>=<span class="st0">"wikipedia"</span> <span class="re2">href</span>=<span class="st0">"http://en.wikipedia.org/wiki/Grep"</span><span class="sy0">></span><span class="kw2">grep</span><span class="sy0"></</span>a<span class="sy0">></span> <span class="re4">$1</span><br /> <span class="kw1">if</span> <span class="br0">&#91;</span> <span class="re4">$?</span> <span class="sy0">!</span>= <span class="nu0"></span> <span class="br0">&#93;</span><br /> <span class="kw1">then</span><br /> <span class="re2">PATH1</span>=<span class="re1">$PATH1</span>:<span class="co1">${PATH_[$i]}</span><br /> <span class="re2">PATH1</span>=$<span class="br0">&#40;</span><span class="kw3">echo</span> <span class="re1">$PATH1</span><span class="sy0">|</span><span class="kw2">tr</span> <span class="re5">-d</span> <span class="co3">\'</span>\\n<span class="co3">\'</span><span class="br0">&#41;</span><br /> <span class="co0">#echo $PATH1</span><br /> <span class="kw1">else</span><br /> <span class="kw3">echo</span> <span class="st0">"Not adding <span class="es3">${PATH_[$i]}</span>"</span><br /> <span class="kw1">fi</span><br /> <span class="kw1">done</span><br /> <span class="kw3">echo</span> <span class="st0">"old path saved in <span class="es2">$PATH_</span>"</span><br /> <span class="kw3">export</span> <span class="re2">PATH</span>=<span class="re1">$PATH1</span>
    </div>
  </td>
</tr>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
\"Enhanced

author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it