2

in my ~/.bashrc in Linux , I have a line that adds some paths to PATH, something like:

PATH=~/mydoc:~/mypython:"$PATH"

The purpose of adding ~/mypython is to use my self-installed python, rather than the system's default one. But now I would like to change back to use the system's default python. Without logout, is there a simple way to remove ~/mypython from the value of PATH?

Tim
  • 98,580
  • 191
  • 570
  • 977
  • 1
    Logout won't do it. When you log in your `~/.bashrc` is automatically executed --- this is how your `PATH` is built in the first place. Just remove `~/mypython` from your `~./bashrc` and execute it: `. ~/.bashrc`. – gardenhead Jan 06 '16 at 02:31

5 Answers5

5
PATH=$(tr ":" "\n" <<<"$PATH" | grep -Fxv "$HOME/mypython" | paste -sd:)
glenn jackman
  • 84,176
  • 15
  • 116
  • 168
2

You can update your path in the shell:

TRIM=~/mypython
export PATH=$(echo "$PATH" | sed -e "s,:$TRIM:,:," -e "s,:$TRIM\$,," -e "s,^$TRIM:,," )

This is more general than your specific case. That could be done with

TRIM=~/mypython
export PATH=$(echo "$PATH" | sed -e "s,:$TRIM:,:," )

I used a temporary variable TRIM to store the tilde-expanded value of ~/mypython because that would not be expanded in the sed command. I also did not use $HOME because (while it is usually the same) it is not guaranteed to have the same value as the expansion of ~ (tilde). You can set HOME to a different directory from your actual home-directory as needed (not generally a Good Thing to do, of course).

For my own use, I use a utility newpath, e.g.,

export PATH=$(newpath -r ~/mypython)
Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
  • “`$HOME` is not guaranteed to have the same value as the expansion of `~`” Yes it is. By POSIX, and also in traditional shells. `~` (where expanded) is a shortcut for `$HOME` (always acting as if in double quotes). `$HOME` may not be the user's home directory since anybody can change it, but `~` follows `$HOME`. What could happen here is that `$HOME` at the time of the `PATH` change isn't the same as what it was at the time `PATH` was set, but whether or not it's accessed through `~` doesn't change anything. P.S. why so complicated (and not robust wrt special-character) with sed? – Gilles 'SO- stop being evil' Jan 06 '16 at 23:02
  • I used a printing character (comma) to make the example easy to read. (Regarding tilde vs `HOME`: I apparently have been working with some non-POSIX shells, and forgot to address the chosen audience). – Thomas Dickey Jan 06 '16 at 23:19
2

Here's a function to remove a PATH component which I think handles all edge cases. (Even the evil ones with empty components.)

remove_from_PATH () {
  while case $PATH in
          "$1") unset PATH; false;;
          "$1:"*) PATH=${PATH#"$1:"};;
          *":$1") PATH=${PATH%":$1"};;
          *":$1:"*) PATH=${PATH%%":$1:"*}:${PATH#*":$1:"};;
          *) false;;
        esac
  do
    :
  done
}

remove_from_PATH ~/mypython

In zsh, it's easier to use the path array.

path=("${(@)path:#"$HOME/mypython"}")

Note that you shouldn't modify PATH in .bashrc.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
1

You can set up a function to edit $PATH in a line-separated format in your vi as if it were a file:

pathEdit(){ 
    export PATH="`printf '%s' "$PATH" |tr : '\n'|
    vipe | tr '\n' : |sed 's/:$//'`"; 
}

If you don't have the vipe from moreutils, you can emulate it with:

#!/bin/sh
set -e
st=0; tmpf=
tmpf="`mktemp`" && exec 3<>"$tmpf" || st="$?"
rm -f "$tmpf"
[ "$st" = 0 ] || exit "$st"
cat >&3
</dev/tty vi "$@" /dev/fd/3 >/dev/tty
cat /dev/fd/3
Petr Skocik
  • 28,176
  • 14
  • 81
  • 141
-1

I'd change your .bashrc and then execute the following:

 . .bashrc

This takes your .bashrc stuff and places it in your current environment.

mdpc
  • 6,736
  • 3
  • 32
  • 46