[英]Include command inside PATH environment variable
Is it possible to include a command inside the PATH, that's run whenever PATH is read? 是否可以在PATH中包含一个命令,该命令在读取PATH时便会运行?
Use case: I want to prepend the output of npm bin
to my path so I have access to local npm package binaries without having to type $(npm bin)/grunt
. 用例:我想将
npm bin
的输出添加到我的路径之前,这样我就可以访问本地npm软件包二进制文件,而不必键入$(npm bin)/grunt
。 If I cd to another node project, I'd want my path updated to point to the new output of npm bin
. 如果我cd到另一个节点项目,则希望更新路径以指向
npm bin
的新输出。
Your best bet would be to update your path on every directory change. 最好的选择是在每次目录更改时更新路径。 Something along these lines ought to do it:
遵循这些思路应该做到这一点:
# define a "PRE_PATH"
export PRE_PATH="/bin:/usr/bin" # <-- put your actual path here
export PATH=$PRE_PATH
function update_my_path() {
local npm_path
npm_path=`npm bin`
if [[ -z $npm_path ]]; then
PATH=${npm_path}:$PRE_PATH
# do you need to rehash after redefining your $PATH? I don't know...
rehash
fi
}
autoload -U add-zsh-hook
add-zsh-hook chpwd update_my_path
an alternative would be to define a bunch of functions such as 另一种方法是定义一堆函数,例如
function grunt() { $(npm bin)/grunt $@ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.