为什么我的PATH被忽略? [英] Why is my PATH ignored?
问题描述
尽管我的Unix PATH包括/Library/TeX/texbin
,但我的PATH的这一部分似乎被忽略了.例如,当我尝试
pdftex --version
我知道
-bash:pdftex:找不到命令
同时
/Library/TeX/texbin/pdftex --version
按预期工作.
类似地,
其中-a pdftex
没有结果.
我的PATH来自两个来源:/private/etc/paths
,其中包含
/Users/Rax/.cabal/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/Rax/bin
以及/private/etc/paths.d/40-XQuartz
和/private/etc/paths.d/TeX
中的两个其他文件,分别包含
/opt/X11/bin
和
/Library/TeX/texbin
这3个文件一起产生预期的PATH
$ echo $PATH
/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin
可以按预期找到其他目录中的所有条目(包括/opt/X11/bin
中的条目),但是最后一个条目似乎被忽略了(至少在查找可执行文件时).
为什么我的PATH的一部分会被忽略?我该如何确保它不存在,以便在那里按预期找到可执行文件?
OS X 10.11.3
在注释中已对此进行了整理,但我将作为记录的答案进行发布:问题是由于PATH
中的不可见字符引起的,这被解释为实际目录名称的一部分.具体来说,它的末尾是一个空格,但是您可以从许多其他不可见字符中获得相同的效果. (我实际上是在猜测path.d中的文件之一是DOS/Windows文本格式,并且在行尾有回车符.)
要使不可见字符更加可见,可以使用printf
或cat -vet
:
$ printf "%q\n" "$PATH"
/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin\
$ echo "$PATH" | LC_ALL=c cat -vet
/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin $
请注意在printf
输出末尾的\
-它后面实际上是一个空格,但是您必须推断-在cat -vet
输出中$
之前的空格.顺便说一句,将$PATH
的引用放在双引号中非常重要,因为没有它们,空格将被剪裁.
如果原来是回车,则显示为:
$ printf "%q\n" "$PATH"
$'/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin\r'
$ echo "$PATH" | LC_ALL=c cat -vet
/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin^M$
在这种情况下,printf
将回车符显示为\r
(并将整个内容包装在$' ... '
中以指示应解释转义符),而cat -vet
将其显示为^M
.>
Although my Unix PATH includes /Library/TeX/texbin
this component of my PATH appears to be ignored. For example, when I try
pdftex --version
I get
-bash: pdftex: command not found
while
/Library/TeX/texbin/pdftex --version
works as expected.
Similarly,
where -a pdftex
gives no results.
My PATH is built from two sources: /private/etc/paths
, which contains
/Users/Rax/.cabal/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/Rax/bin
and two additional files in /private/etc/paths.d/40-XQuartz
, and /private/etc/paths.d/TeX
, which contain, respectively
/opt/X11/bin
and
/Library/TeX/texbin
Together these 3 files result in the expected PATH
$ echo $PATH
/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin
All the entries in the other directories are found as expected (inclusion those in /opt/X11/bin
) but the last entry appears to be ignored (at least when locating executables).
Why is part of my PATH being ignored? How do I ensure that it is not, so that executables there are found as expected?
OS X 10.11.3
This got sorted out in the comments, but I'll post as an answer for the record: the problem was due to an invisible character in the PATH
, that was being interpreted as part of the actual directory name. Specifically, it was a space at the end, but you could get the same effect from a number of other invisible characters. (I was actually guessing that one of the files in paths.d was in DOS/Windows text format, and had a carriage return at the end of the line.)
To make invisible characters more visible, you can use printf
or cat -vet
:
$ printf "%q\n" "$PATH"
/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin\
$ echo "$PATH" | LC_ALL=c cat -vet
/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin $
Note the \
at the end of the printf
output -- it's actually followed by a space, but you have to infer that -- and the space before the $
in the cat -vet
output. BTW, it's very important to put the reference to $PATH
in double-quotes, since without them the space would've been trimmed.
If it had been a carriage return instead, here's what it would have looked like:
$ printf "%q\n" "$PATH"
$'/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin\r'
$ echo "$PATH" | LC_ALL=c cat -vet
/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin^M$
In this case, printf
shows the carriage return as \r
(and wraps the whole thing in $' ... '
to indicate that escapes should be interpreted), while cat -vet
shows it as ^M
.
这篇关于为什么我的PATH被忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!