Linux 向 ZSH 中的 PATH 变量添加新条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11530090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a new entry to the PATH variable in ZSH
提问by David Barreto
I'm using zsh and I'm trying to add a new entry (/home/david/pear/bin
) to the PATH
variable but I don't know how.
我正在使用 zsh 并且我正在尝试向变量添加一个新条目 ( /home/david/pear/bin
)PATH
但我不知道如何。
The thing that confuses me the most is that there's not a single
reference to a PATH
variable in my ~/.zshrc
file, but doing echo $PATH
returns:
最让我困惑的是PATH
我的~/.zshrc
文件中没有对变量的单一引用,而是echo $PATH
返回:
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
So a PATH
variable is being set somewhere.
所以在PATH
某处设置了一个变量。
采纳答案by Linuxios
Here:
这里:
export PATH=/home/david/pear/bin:$PATH
EDIT: This does work, but ony's answerbelow is better, as it takes advantage of the structured interface ZSH provides for variables like $PATH
. This approach is standard for bash
, but as far as I know, there is no reason to use it when ZSH provides better alternatives.
编辑:这确实有效,但下面的答案更好,因为它利用了 ZSH 为诸如$PATH
. 这种方法是 的标准bash
,但据我所知,当 ZSH 提供更好的替代方案时,没有理由使用它。
回答by ony
Actually, using ZSH allows you to use special mapping of environment variables. So you can simply do:
实际上,使用 ZSH 允许您使用环境变量的特殊映射。所以你可以简单地做:
# append
path+=('/home/david/pear/bin')
# or prepend
path=('/home/david/pear/bin' $path)
# export to sub-processes (make it inherited by child processes)
export PATH
For me that's a very neat feature which can be propagated to other variables. Example:
对我来说,这是一个非常巧妙的功能,可以传播到其他变量。例子:
typeset -T LD_LIBRARY_PATH ld_library_path :
回答by Micah Elliott
You can append to your PATH
in a minimal fashion. No need for
parentheses unless you're appending more than one element. It also
usually doesn't need quotes. So the simple, short way to appendis:
您可以以PATH
最小的方式附加到您的。除非您附加多个元素,否则不需要括号。它通常也不需要引号。所以简单、简短的追加方法是:
path+=/some/new/bin/dir
Common usage
常见用法
Then the common pattern for testing a new script/executable becomes:
然后测试新脚本/可执行文件的常见模式变为:
path+=$PWD/.
# or
path+=$PWD/bin
This lower-case syntax is using path
as an array, yet also
affects its upper-case partner equivalent, PATH
(to which it is
"bound" via typeset
).
这种小写语法path
用作array,但也会影响它的大写伙伴等价物,PATH
(它通过“绑定” typeset
)。
(Notice that no :
is needed/wanted as a separator.)
(注意,没有:
需要/希望作为隔板。)
Related tidbits
相关花絮
Treating path
this way (as an array) also means: no need to do a
rehash
to get the newly pathed commands to be found.
以path
这种方式(作为数组)处理也意味着:无需执行 a
rehash
即可找到新路径的命令。
Also take a look at vared path
as a dynamic way to edit path
and other things.
也看看vared path
作为一种动态的方式来编辑path
和其他东西。
You may only be interested in path
for this question, but since
we're talking about exports and arrays, note that
arrays generally cannot be exported.
您可能只path
对这个问题感兴趣,但由于我们谈论的是导出和数组,请注意
通常无法导出数组。
You can even prevent PATH
from taking on duplicate entries(refer to
thisand this):
您甚至可以防止PATH
重复输入(请参阅
this和this):
typeset -U path
回答by Siva Praveen
one liner, without opening ~/.zshrc
file
一个衬垫,无需打开~/.zshrc
文件
echo -n 'export PATH=~/bin:$PATH' >> ~/.zshrc
or
或者
echo -n 'export PATH=$HOME/bin:$PATH' >> ~/.zshrc
To see the effect, do source ~/.zshrc
in the same tab or open a new tab
要查看效果,请source ~/.zshrc
在同一选项卡中执行或打开新选项卡
回答by Dimitar
OPTION 1:Add this line to ~/.zshrc:
选项 1:将此行添加到 ~/.zshrc:
export "PATH=$HOME/pear/bin:$PATH"
After that you need to run source ~/.zshrc
in order your changes to take affect OR close this window and open a new one
之后,您需要运行source ~/.zshrc
以使您的更改生效或关闭此窗口并打开一个新窗口
OPTION 2:execute it inside the terminal console to add this path only to the current terminal window session. When you close the window/session, it will be lost.
选项 2:在终端控制台内执行它以仅将此路径添加到当前终端窗口会话。当您关闭窗口/会话时,它将丢失。
回答by saneryee
Added path to ~/.zshrc
sudo vi ~/.zshrc
add new path
export PATH="$PATH:[NEW_DIRECTORY]/bin"
Update ~/.zshrc
Save ~/.zshrc
source ~/.zshrc
Check PATH
echo $PATH
添加到 ~/.zshrc 的路径
sudo vi ~/.zshrc
添加新路径
export PATH="$PATH:[NEW_DIRECTORY]/bin"
更新 ~/.zshrc
保存 ~/.zshrc
source ~/.zshrc
检查路径
echo $PATH