0

I have the following PATH.

PATH=/home/josemserrajr/anaconda3/bin:/home/josemserrajr/bin:/homejosemserrajr/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us

I want to unset one of these variables is this the following way to do it:

unset PATH=/homejosemserrajr/anaconda3/bin:
3

2 Answers 2

2

PATH is actually a single variable, which has a string value. The value consists of directory paths, which are separated with colons (:).

Usually, your PATH variable is defined and exported in the ~/.profile file, which you can edit with your favorite text editor. If you cannot find the place where /home/josemserrajr/anaconda3/bin is added, you can reset the PATH variable and explicitly remove that path from the string with a command such as the following:

export PATH="$(echo "$PATH" | sed 's|/home/josemserrajr/anaconda3/bin||g' | sed 's|^:*||' | sed 's|:*$||')"

The command uses a bit of bash and sed to do the following:

  1. Remove all occurrences of the string /home/josemserrajr/anaconda3/bin from the current PATH value.
  2. Remove any (0 or more) leading colons (matched by the regular expression ^:*) from the resulting string of step 1.
  3. Remove any (0 or more) trailing colons (matched by the regular expression :*$) from the resulting string of step 2.
  4. Save the resulting string from step 3 into the PATH variable (overwriting the old value).
  5. Export the new PATH value to your environment, so that applications can access it.

By adding this command to the end of your ~/.profile file you should be able to remove the unwanted path from your PATH variable.

2

You can use parameter expansion to remove a substring from the value of a variable. For example, given

echo "$var"
/home/josemserrajr/anaconda3/bin:/home/josemserrajr/bin:/homejosemserrajr/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us

then to remove the first instance of substring /homejosemserrajr/anaconda3/bin:, you could use

$ echo "${var/\/homejosemserrajr\/anaconda3\/bin:}"
/home/josemserrajr/anaconda3/bin:/home/josemserrajr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us

(note the backslashes to escape literal slashes in the path). To apply this to your PATH,

PATH="${PATH/\/homejosemserrajr\/anaconda3\/bin:}"

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.