1

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?

  • 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 6 '16 at 2:31
4
PATH=$(tr ":" "\n" <<<"$PATH" | grep -Fxv "$HOME/mypython" | paste -sd:)
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)
  • $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 Jan 6 '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 6 '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.

  • Thanks. WHere should I modify PATH? – Tim Jan 7 '16 at 2:33
  • @Tim Click. The. Link. – Gilles Jan 7 '16 at 2:44
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
-1

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

 . .bashrc

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

  • Thanks. How does it remove ~/mypython from PATH? – Tim Jan 6 '16 at 1:13
  • EDIT your .bashrc file where the PATH variable is present. – mdpc Jan 6 '16 at 21:53
  • That won't work since the snippet shown in the question adds to PATH, it doesn't set it to a fixed value. – Gilles Jan 6 '16 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.