使用grep在$PATH变量中查找条目。

huangapple go评论82阅读模式
英文:

use grep to find an entry in the $PATH variable

问题

我已经找到了许多关于如何使用grep来搜索变量中的值,但没有找到PATH变量的实际内容中的特定字符串的答案。我在Mac上,尝试确定"brew"是否在我的PATH中,我认为我可以使用grep来做到这一点...我确信这是可能的,但是我尝试过的一切都不起作用,所以有人可以告诉我吗?以下是我认为会起作用的内容,但它只是列出了PATH变量的内容。

grep brew "${PATH}"

我还尝试过

grep brew -- "${PATH}"

它只是做了同样的事情。我漏掉了什么吗?

英文:

I have found many answers to using grep to search for the value in a variable within the contents of a file but not finding a particluar string in the actual contents of the $PATH variable. I'm on a mac and was trying to determine if "brew" was in my PATH and thought surely I can just use grep to do this... and I'm sure it's possible but nothing I've tried works so can someone please enlighten me? The following was what I thought would work but it just lists the contents of the PATH variable.

grep brew "${PATH}"

I also tried

grep brew -- "${PATH}"

which just did the same thing. What am I missing?

答案1

得分: 1

  1. 我的二进制文件位于哪里?

假设您正在搜索二进制文件 brew,它位于哪里?答案是使用 which 命令。which 命令会打印在 shell 提示符上输入时将要执行的可执行文件的完整路径。它会通过在 PATH 列表中列出的目录中搜索可执行文件或脚本来执行此操作:

$ which brew

如果您想列出所有条目,即有时目录会掩盖其他目录,您可以使用 --all 标志:

$ which --all brew
  1. 路径中是否有与正则表达式 ere 匹配的路径项:
$ awk -v RS=":" '/ere/' <<<$PATH
英文:
  1. Where is my binary located?

Assume you are searching for the binary brew, where is it located? The answer here is to make use of which. Which will print the full path of the executable that would have been executed when entered on the shell prompt. It will do this by searching for an executable or script in the directories listed in PATH:

$ which brew

If you want to list all entries, i.e. sometimes directories mask others, you can use the --all flag:

$ which --all brew
  1. Is there a path-entry in path that matches the regular expression ere:
$ awk -v RS=&quot;:&quot; &#39;/ere/&#39; &lt;&lt;&lt;$PATH

huangapple
  • 本文由 发表于 2023年6月26日 11:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定