Remove some path from the PATH variable temporarily.

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 🙂

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
#!/bin/bash -
#===============================================================================
#
#          FILE:  remove_path.sh
#
#         USAGE:  ./remove_path.sh
#
#   DESCRIPTION:  Add a path from the $PATH variable
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  <a class="zem_slink" title="Amit Agarwal" rel="me" href="http://amit-agarwal.co.in">Amit Agarwal</a> (AKA), [email protected]
#       COMPANY:  Individual
#       VERSION:  1.0
#       CREATED:  08/08/2010 02:26:16 PM IST
#      REVISION:  ---
#===============================================================================
if [[ $# < 1 ]]
then
echo "Not enough arguments"
exit -999
fi
unset PATH_ PATH1
export PATH_=($(echo $PATH|sed \'s/:/ /g\'))
for i in `seq 1 ${#PATH_}`
do
echo ${PATH_[$i]}
echo ${PATH_[$i]}|<a class="zem_slink" title="Grep" rel="wikipedia" href="http://en.wikipedia.org/wiki/Grep">grep</a> $1
if [ $? != 0 ]
then
PATH1=$PATH1:${PATH_[$i]}
PATH1=$(echo $PATH1|tr -d \'\\n\')
#echo $PATH1
else
echo "Not adding ${PATH_[$i]}"
fi
done
echo "old path saved in $PATH_"
export PATH=$PATH1

\"Enhanced