Visit Our UNIX and Linux User Community


remove a path from PATH environment variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove a path from PATH environment variable
# 8  
Old 10-23-2009
Code:
echo $PATH |awk -F":" -v v="$rmv" '{for(i=1;i<=NF;i++)if($i==v){$i=""}}1' OFS=":"

on Solaris use nawk
# 9  
Old 10-23-2009
Is there any script which will work on all the machines?

On Solaris INTEL machine, the previous script behaves in below manner

Code:
$echo $PATH
/usr/local/bin:/usr/bin:test/rmve:/usr/games
$PATH=$(echo $PATH| sed -e 's!'$rmv'!!' -e 's/::/:/')
syntax error: `testvar=$' unexpected
$echo $PATH| sed -e 's!'$rmv'!!' -e 's/::/:/'
/usr/local/bin:/usr/bin:/usr/games

The script works just fine, but it fails to reassign PATH
# 10  
Old 10-23-2009
Quote:
Originally Posted by madhu84
I need a shell script which will remove path (test/rmve) from PATH (/usr/local/bin:/usr/bin:test/rmve:/usr/games) and update the PATH as /usr/local/bin:/usr/bin:/usr/games

Code:
rmpath() #@ remove directory or directories from $PATH
{        #@ USAGE: rmpath dir ...
    for p in "$@"
    do
      p=${p%"${p##*[!/]}"} ## remove trailing slashes
      case $PATH in
          "$p":*) PATH=${PATH#$p:} ;; ## at beginning of PATH
          *:"$p") PATH=${PATH%:$p} ;; ## at end of PATH
          *:"$p":*) PATH=${PATH%":$p"*}${PATH##*"$p"} ;;
      esac
    done
    export PATH
}

This User Gave Thanks to cfajohnson For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #1
Difficulty: Medium
The UNIX and Linux Forums (UNIX.com) was founded in 1985.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function to remove the directories from PATH variable

Hello, From the URL https://www.unix.com/shell-programming-scripting/121303-remove-path-path-environment-variable-2.html I got a function to remove the directories from a path. but looks like this isnt quite working.. i am also not able to post the comments in the thread as it is closed. ... (6 Replies)
Discussion started by: satishkumar432
6 Replies

2. Ubuntu

PATH environment variable

PATH is an environment variable. When I open a terminal say terminal 1 and set some path in PATH variable it gets set which I can see using ech $PATH. But when I open a new terminal say terminal 2 and fire echo $PATH why cannot I see the same output as seen in terminal terminal 1? Why the path... (4 Replies)
Discussion started by: rupeshkp728
4 Replies

3. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

4. Shell Programming and Scripting

Appending a path in user's PATH variable

Hello Folks, I want to append a path in user's PATH variable which should be available in current session. Background Numerous persons will run a utility. Aim is to add the absolute path of the utility the first time it runs so that next runs have the PATH in env & users can directly run... (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

5. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

6. UNIX for Dummies Questions & Answers

Path Environment

How do we change path environment? (2 Replies)
Discussion started by: mehmetned
2 Replies

7. Shell Programming and Scripting

problem in getting the path of environment variable set in bashrc in my shell script

hi all i have joined new to the group. i have set an variable in my bashrc file. .bashrc PROGHOME=/home/braf/braf/prog export PROGHOME but while using it in my shell script its path is not taken and i had to explicitly give the export command to set the path. in my script... (8 Replies)
Discussion started by: krithika
8 Replies

8. Shell Programming and Scripting

:: in PATH environment variable

whats the meaning of :: colon in PATH environment? /usr/local/bin:/usr/bin:/usr/local/gnu/bin::.:/usr/local/bin:/usr/bin:/usr/local/gnu/bin:/usr/local/bin and whats the meaning of // in PATH ? (1 Reply)
Discussion started by: gfhgfnhhn
1 Replies

9. Shell Programming and Scripting

Path Environment Variable

Hi..... I'm kind of new to c programming in Unix...need help here. Supposed to write a source code to support Path environment variable for my programming assignment for Spring semester. but i'm kind of stuck. Could anyone out there assist me? prompt> /bin/ls My program could output... (3 Replies)
Discussion started by: tancy
3 Replies

10. UNIX for Advanced & Expert Users

How does the PATH and MANPATH environment variable get set?

Hi, How does the PATH and MANPATH environment variable get set? I want to add "/opt/SUNWspro/bin" to the search path for all the users. Where can I access this variable. I know in my home directory, depend on which shell I use, there are files such as .profile and .cshrc which I can edit to... (3 Replies)
Discussion started by: vtran4270
3 Replies

Featured Tech Videos