Remove Redundant Paths from $Path Variable

Remove redundant paths from $PATH variable

You just execute:

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

that would be for the current session, if you want to change permanently add it to any .bashrc, bash.bashrc, /etc/profile - whatever fits your system and user needs.

Note: This is for Linux. We'll make this clear for new coders. (` , ') Don't try to SET = these.

Remove unwanted path name from %path% variable via batch

You can try something like this :

@echo off&cls
setlocal EnableDelayedExpansion
set $line=%path%
set $line=%$line: =#%
set $line=%$line:;= %

for %%a in (%$line%) do echo %%a | find /i "oracle" || set $newpath=!$newpath!;%%a
set $newpath=!$newpath:#= !
echo set path=!$newpath:~1!

I putted an echo to the last line. Check the result and If it's OK for you, remove it.

Duplicates in windows environment path

The paths you mention are system paths. They should stay in the PATH variable in the system scope. You can remove the duplicates in the PATH variable of the user scope, but you should reboot and check, if every application is still working (not because you deleted a duplicate, but to make sure you didn't delete something wrong by mistake). As usual, backup your PATH variables somewhere, before you start.

Duplicates inside each scope can always be safely removed. The list is split at every semicolon and each resulting path in the list is searched. If there are duplicates, the same search simply executes twice in the worst case. In the best case, the system might recognize the duplicates (I'm not sure if this happens), but this would mean additional effort for recognizing. So your statement on slowing down is correct in any case.

The reason for you duplicates (if it wasn't you at least) might probably be some application you installed somewhen, which edited the PATH variable improperly.

How to remove a directory from the %PATH% variable by cmd

I'd investigate why that terminal % persists - it doesn't on my machine.

Your problem appears to be that since the resultant path contains spaces, you need to quote the value to be assigned. It would also help if you were to use the correct syntax for setx - the = is at best superfluous.

call setx path "%%path:%cut%=%%"

should work happily, although I'd suggest you set a dummy variable for testing, just in case as path is a critical variable.

What is the most elegant way to remove a path from the $PATH variable in Bash?

A minute with awk:

# Strip all paths with SDE in them.
#
export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}'`

Edit: It response to comments below:

$ export a="/a/b/c/d/e:/a/b/c/d/g/k/i:/a/b/c/d/f:/a/b/c/g:/a/b/c/d/g/i"
$ echo ${a}
/a/b/c/d/e:/a/b/c/d/f:/a/b/c/g:/a/b/c/d/g/i

## Remove multiple (any directory with a: all of them)
$ echo ${a} | awk -v RS=: -v ORS=: '/a/ {next} {print}'
## Works fine all removed

## Remove multiple including last two: (any directory with g)
$ echo ${a} | awk -v RS=: -v ORS=: '/g/ {next} {print}'
/a/b/c/d/e:/a/b/c/d/f:
## Works fine: Again!

Edit in response to security problem: (that is not relevant to the question)

export PATH=$(echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}' | sed 's/:*$//')

This removes any trailing colons left by deleting the last entries, which would effectively add . to your path.

Remove wrong PATH from my environment

If you have set PATH on your Shell's environment just exiting the terminal session will reset it.

What is the most elegant way to remove a path from the $PATH variable in Bash?

A minute with awk:

# Strip all paths with SDE in them.
#
export PATH=`echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}'`

Edit: It response to comments below:

$ export a="/a/b/c/d/e:/a/b/c/d/g/k/i:/a/b/c/d/f:/a/b/c/g:/a/b/c/d/g/i"
$ echo ${a}
/a/b/c/d/e:/a/b/c/d/f:/a/b/c/g:/a/b/c/d/g/i

## Remove multiple (any directory with a: all of them)
$ echo ${a} | awk -v RS=: -v ORS=: '/a/ {next} {print}'
## Works fine all removed

## Remove multiple including last two: (any directory with g)
$ echo ${a} | awk -v RS=: -v ORS=: '/g/ {next} {print}'
/a/b/c/d/e:/a/b/c/d/f:
## Works fine: Again!

Edit in response to security problem: (that is not relevant to the question)

export PATH=$(echo ${PATH} | awk -v RS=: -v ORS=: '/SDE/ {next} {print}' | sed 's/:*$//')

This removes any trailing colons left by deleting the last entries, which would effectively add . to your path.

How to keep from duplicating path variable in ksh

I use this oneliner, but it depends on gawk.

PATH=$(echo $PATH|tr ":" "\n"|gawk '!($0 in a) { a[$0];print}'|paste -sd: - )


Related Topics



Leave a reply



Submit