$ PATH中是否有Tilde char('〜')? (例如:PATH ='〜/bin':/usr/bin:/bin) [英] Tilde char ('~') in $PATH? (e.g.: PATH='~/bin':/usr/bin:/bin)
问题描述
根据我的测试,bash
对$PATH
中的~
非常满意,但/usr/bin/which
并不满意. 是否有与此相关的标准(例如POSIX)?
Based on my testing, bash
is very happy with ~
in $PATH
but /usr/bin/which
is not. Is there any standard (like POSIX) regarding this?
示例:
[STEP 100] # echo $BASH_VERSION
4.4.12(1)-release
[STEP 101] # echo $PATH
/root/bin:/usr/sbin:/usr/bin:/sbin:/bin
[STEP 102] # /usr/bin/which passh
/root/bin/passh
[STEP 103] # type passh
passh is /root/bin/passh
[STEP 104] #
[STEP 105] # PATH='~/bin':/usr/sbin:/usr/bin:/sbin:/bin
[STEP 106] # echo $PATH
~/bin:/usr/sbin:/usr/bin:/sbin:/bin
[STEP 107] # /usr/bin/which passh
[STEP 108] # hash -r
[STEP 109] # hash
hash: hash table empty
[STEP 110] # type passh
passh is /root/bin/passh
[STEP 111] #
正如我们所看到的,在步骤107中,which
再也找不到passh
了,但是在步骤110中,在清除哈希表(hash -r
)之后,bash
仍然可以找到passh
.
As we can see, in step 107 which
cannot find passh
any more but in step 110 bash
can still find passh
after I clear the hash table (hash -r
).
推荐答案
您在PATH
变量中有此部分:'~/bin'
You have this part in PATH
variable: '~/bin'
在此处引用~
,并且不会扩展.可以由bash
内置实用程序(如type
)处理,但是which
是一个外部程序,不会扩展用~
引用.
Here ~
is quoted and doesn't get expanded. This can be handled by bash
builtin utilities like type
but which
is an external program that doesn't expand quoted ~
.
如果要扩展~
,则将其保留在引号之外:
If you want ~
to expand then keep it outside the quotes as:
PATH=~/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin
或使用$HOME
:
PATH="$HOME/bin":/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin
这篇关于$ PATH中是否有Tilde char('〜')? (例如:PATH ='〜/bin':/usr/bin:/bin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!