从 Bash 中的 $PATH 变量中删除路径的最优雅方法是什么? [英] What is the most elegant way to remove a path from the $PATH variable in Bash?

查看:49
本文介绍了从 Bash 中的 $PATH 变量中删除路径的最优雅方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者更一般地说,如何从 Bash 环境变量中以冒号分隔的列表中删除项目?

Or more generally, how do I remove an item from a colon-separated list in a Bash environment variable?

我以为我在几年前看到了一种简单的方法,使用更高级的 Bash 变量扩展形式,但如果是这样,我已经忘记了.谷歌的快速搜索令人惊讶地发现很少有相关结果,而且没有一个我会称之为简单"或优雅"的.比如分别使用sed和awk的两种方法:

I thought I had seen a simple way to do this years ago, using the more advanced forms of Bash variable expansion, but if so I've lost track of it. A quick search of Google turned up surprisingly few relevant results and none that I would call "simple" or "elegant". For example, two methods using sed and awk, respectively:

PATH=$(echo $PATH | sed -e 's;:?/home/user/bin;;' -e 's;/home/user/bin:?;;')
PATH=!(awk -F: '{for(i=1;i<=NF;i++){if(!($i in a)){a[$i];printf s$i;s=":"}}}'<<<$PATH)

没有什么直截了当的存在吗?有没有类似于 Bash 中的 split() 函数的东西?

Does nothing straightforward exist? Is there anything analogous to a split() function in Bash?

更新:
看起来我需要为我故意含糊的问题道歉;我对解决特定用例的兴趣不大,而是对引发良好的讨论感兴趣.幸运的是,我拿到了!

Update:
It looks like I need to apologize for my intentionally-vague question; I was less interested in solving a specific use-case than in provoking good discussion. Fortunately, I got it!

这里有一些非常聪明的技巧.最后,我在我的工具箱中添加了以下三个函数.神奇发生在 path_remove 中,它主要基于 Martin York 对 awk 的 RS 变量的巧妙使用.

There are some very clever techniques here. In the end, I've added the following three functions to my toolbox. The magic happens in path_remove, which is based largely on Martin York's clever use of awk's RS variable.

path_append ()  { path_remove $1; export PATH="$PATH:$1"; }
path_prepend () { path_remove $1; export PATH="$1:$PATH"; }
path_remove ()  { export PATH=`echo -n $PATH | awk -v RS=: -v ORS=: '$0 != "'$1'"' | sed 's/:$//'`; }

其中唯一真正的问题是使用 sed 删除尾随的冒号.不过,考虑到 Martin 的其余解决方案是多么简单,我很愿意接受它!

The only real cruft in there is the use of sed to remove the trailing colon. Considering how straightforward the rest of Martin's solution is, though, I'm quite willing to live with it!

相关问题:如何在 shell 脚本中操作 $PATH 元素?

推荐答案

使用 awk 的一分钟:

A minute with awk:

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

它回应以下评论:

$ 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!

针对安全问题进行(与问题无关)

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.

这篇关于从 Bash 中的 $PATH 变量中删除路径的最优雅方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆