18.04 - 在Linux OS中删除$PATH中一个路径

分享于 

3分钟阅读

ubuntu

  繁體

问题:

我在安装JAVA,取消了JAVA_HOME变量,并且试图删除路径/opt/jdk-14/bin

sudo grep -rl "/opt/jdk-14/bin" /etc

我检查了 ~/.profile, ~/.bashrc, ~/.bash_profile, /etc/bashrc ,但是没有一个文件包含路径/opt/jdk-14/bin

我又重新安装了java,同样的路径现在重复两次。

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/jdk-14/bin:/home/dell/flutter/bin:/opt/jdk-14/bin


答案1:

在/etc/profile.d/中创建一个.sh文件,并添加以下脚本,


rpath() {


 set -- $(printf '%sn' "$@" | sed -e 's/[^^]/[&]/g' -e 's/^/^/g')


 for path in "$@"; do


 PATH=$(echo "$PATH" | sed -r -e 's#(^|:)'"$path"'/?(:|$)#2#g' -e 's/^://')


 done


 unset path


}


rpath '/opt/jdk-14/bin'


unset -f rpath





答案3要查找设置路径的位置,请使用:

sudo grep -rnw --exclude-dir={media,mnt,proc,root,run,sys,/tmp,tmpfs,var} '/' -e "/opt/jdk-14/bin"



临时删除当前会话的路径

可以使用sed将不需要的路径替换成空,首先查看当前$PATH变量的内容:


$ echo $PATH



/home/rick/bin:/home/rick/.local/bin:/mnt/e/bin:/mnt/e/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin::/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin



假设要删除的路径是/snap/bin:,然后使用:


$ NewPath=$(echo "$PATH" | sed 's#/snap/bin:##')



现在看看新路径:


$ echo $NewPath



/home/rick/bin:/home/rick/.local/bin:/mnt/e/bin:/mnt/e/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games::/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin





路径  
相关文章