PATH应该包含目录或二进制文件的完整路径吗? [英] Should PATH contain directories or full paths to binaries?
问题描述
我正在尝试设置正确的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
还是其他东西?
这本身不是堆栈溢出的合适问题. 我希望将其关闭为通用计算"或太宽泛"; 但是初学者经常需要 answer ,因此希望不要将其删除.
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.
当您在外壳中键入ls
并将PATH
设置为/usr/local/bin/ls:/usr/bin/ls
时,您的外壳将……
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 …
-
…寻找路径为
/usr/local/bin/ls/ls
的可执行文件(请注意末尾的双ls
).
… look for an executable with the path
/usr/local/bin/ls/ls
(note the doublels
at the end).
由于该路径在您的系统上不存在,因此您的外壳程序将继续查找路径为/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
的内容.
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
比/b/c1/
更喜欢/a/c1
,但同时又比/a/c2
更喜欢/b/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
包含的可执行文件不只是c1
和c2
的情况下将它们包括在内.
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屋!