PATH 是否应该包含二进制文件的目录或完整路径? [英] Should PATH contain directories or full paths to binaries?

查看:13
本文介绍了PATH 是否应该包含二进制文件的目录或完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置正确的 PATH,但我想知道它应该包含什么.如果我有

I am trying to set up a correct PATH, but I'm wondering what it should contain. If I have

/usr/bin/ls
/usr/local/bin/ls

我想使用 /usr/local/bin 中的那个,我应该使用以下哪个?

and I want to prefer the one in /usr/local/bin, which of the following should I use?

PATH=/usr/local/bin/ls:/usr/bin/ls
PATH=/usr/local/bin:/usr/bin

或者完全是别的什么?

这不是本身适合 Stack Overflow 的问题.我希望这会被关闭为通用计算或太宽泛;但是答案是初学者经常需要的,所以我希望这个不会被删除.

This is not per se a suitable question for Stack Overflow. I expect this to be closed as General Computing or Too Broad; but the answer is frequently needed by beginners, so I hope this won't be deleted.

推荐答案

PATH 仅适用于目录,不适用于单个文件

来自 POSIX 标准(重点是我的)

PATH works only with directories, not with single files

From the POSIX standard (emphasis mine)

路径
此变量应表示某些函数和实用程序在搜索仅由文件名已知的可执行文件时应用的路径前缀序列.前缀应由冒号 (':') 分隔.[...] 应从头到尾搜索列表,将文件名应用于每个前缀,直到找到具有指定名称和适当执行权限的可执行文件.

PATH
This variable shall represent the sequence of path prefixes that certain functions and utilities apply in searching for an executable file known only by a filename. The prefixes shall be separated by a colon ( ':' ). [...] The list shall be searched from beginning to end, applying the filename to each prefix, until an executable file with the specified name and appropriate execution permissions is found.

当您在 shell 中输入 ls 并且您的 PATH 设置为 /usr/local/bin/ls:/usr/bin/ls 那么你的 shell 将......

When you type in ls into your shell and your PATH is set to /usr/local/bin/ls:/usr/bin/ls then your shell will …

  1. ... 寻找路径为 /usr/local/bin/ls/ls 的可执行文件(注意末尾的双 ls).

  1. … look for an executable with the path /usr/local/bin/ls/ls (note the double ls at the end).

由于该路径在您的系统中不存在,您的 shell 将继续寻找路径为 /usr/bin/ls/ls(双 ls代码>再次).该路径也不存在.

As that path does not exist on your system your shell will proceed to look for an executable with the path /usr/bin/ls/ls (double ls again). That path also doesn't exist.

shell 无法使用 PATH 中的所有路径找到可执行文件,因此您的 shell 将打印类似 bash: ls: command not found 的内容.p>

The shell couldn't find an executable using all paths in PATH so your shell will print something like bash: ls: command not found.

那么,我们学到了什么?PATH 列出的路径必须 是目录.您不能列出单个文件.因此,您的情况的正确答案是 PATH=/usr/local/bin:/usr/bin.

So, what did we learn? Paths listed by PATH have to be directories. You cannot list single files. Therefore the correct answer in your case is PATH=/usr/local/bin:/usr/bin.

想象一下以下情况.您有两个版本的程序 c1 和两个版本的程序 c2.不同的版本存储在目录/a//b/ 中.

Imagine the following situation. You have two versions of program c1 and two versions of program c2. The different versions are stored in the directories /a/ and /b/.

/a/c1
/a/c2
/b/c1
/b/c2

我们如何设置 PATH 使 /a/c1 优先于 /b/c1/ 但同时/b/c2 超过 /a/c2?

How can we set PATH to prefer /a/c1 over /b/c1/ but at the same time /b/c2 over /a/c2?

遗憾的是,没有办法直接实现这一点,因为我们只能在 PATH 中指定目录.我们必须移动/重命名一些文件或创建符号链接并使用路径内的符号链接.一种可能的解决方案:

Sadly, there is no way to achieve this directly as we can only specify directories in PATH. We have to move/rename some files or create symlinks and use the symlinks inside the paths. One possible solution:

mkdir /c
ln -s /a/c1 /c/c1
ln -s /b/c2 /c/c2
export PATH=/c:/a:/b

尾随 :/a:/b 在这里不是必需的.我将它们包含在内的假设是 /a/b 包含更多的可执行文件,而不仅仅是 c1c2.

The trailing :/a:/b is not really necessary here. I included them under the assumption that /a and /b contain more executables than just c1 and c2.

这篇关于PATH 是否应该包含二进制文件的目录或完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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