gpt4 book ai didi

How can I change PATH variable in zsh?(如何在zsh中更改PATH变量?)

转载 作者:bug小助手 更新时间:2023-10-26 20:47:17 27 4
gpt4 key购买 nike



I want to change my PATH variable in zsh.

我想更改zsh中的PATH变量。


Problem: I don't understand where in the .zshrc file I have to make modifications.

问题:我不知道我必须在.zshc文件中的什么位置进行修改。


Normally, I would look for the assignment to the PATH variable and set the values from scratch how I would like them to be (leaving all the systems binaries directories untouched).

通常,我会查找对PATH变量的赋值,并从头开始设置我想要的值(保留所有系统二进制文件目录不变)。


The first lines in my .zshrc file are as follows:

我的.zshc文件中的前几行如下:


# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/Users/Sam/oh-my-zsh"

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/13/bin

etc.


My actual PATH variable is:

实际的PATH变量是:


/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/13/bin

I want to delete the directory where python3.8 is in, it's redundant.

我想删除python3.8所在的目录,它是多余的。


My questions:

我的问题:



  1. Do I have to change line 2 or line 7 in my .zshrc file?

  2. Line 2 is commented out...is it executed anyway at the start of the terminal?

  3. I have tried to comment out line 7. But the postgres directory still remained in my PATH variable which I don't understand.


更多回答

This would be better asked on SuperUser or Unix&Linux SE. However I doubt this hasn't already bee answered several times in several flavors there.

在超级用户或Unix和Linux SE上问这个问题会更好。然而,我怀疑这个问题是否已经在那里以几种风格得到了多次回答。

Did you check PATH in new session?

您在新会话中检查路径了吗?

优秀答案推荐

The .zshrc is located in the home dir. The dot at the beginning keeps it hidden. Type ls -a from ~ directory to see it. To edit just run vim, nvim, etc as usual.

.zshc位于主目录中。开头的圆点将其隐藏起来。在~目录中输入ls-a即可查看。要编辑,只需像往常一样运行vim、nvim等。


nvim ~/.zshrc

This is the command for Neovim. For your editor, sub nvim for the proper command.

这是Neovim的命令。对于您的编辑,请使用subnvim以获取正确的命令。


Once you get in, you need only add the same export command that you would add from the command line.

一旦进入,您只需添加与从命令行添加的相同的导出命令。


export PATH=$PATH:/whatever/you/are/adding

EDIT

编辑


To remove a path variable:

要删除路径变量,请执行以下操作:



  • First, run the command:


echo $PATH

from the command line.

从命令行。



  • Next, Copy the output to clipboard.

    接下来,将输出复制到剪贴板。



  • Finally, at the very end of the .zshrc file, add the following:

    最后,在.zshc文件的末尾添加以下内容:




export PATH=<paste-what-you-copied-here>

Because you didn't reference $PATH after the =, this will set the path to EXACTLY what you pasted, no more, no less. Adding $PATH: as in the first example will just add whatever to the end of what is already there.

因为您没有在=之后引用$PATH,所以这会将路径设置为您粘贴的内容,不会更多,也不会更少。添加$PATH:与第一个示例一样,只会在已有内容的末尾添加任何内容。


Since this gives you access to every item in the path array, deleting is just a matter of a literal highlight/select and of whatever you want deleted.

由于这使您可以访问路径数组中的每一项,因此删除只是一个字面上的突出显示/选择和您想要删除的任何内容的问题。


Finally, be sure that there is only one place in the file where you are editing PATH. If there are more than one, the result can be confusing to say the least.

最后,确保文件中只有一个地方可以编辑路径。如果有不止一个,结果至少可以说是令人困惑的。


That said, I believe the script runs top-to-bottom, so only the last mention should persist. You can take advantage of this in some situations, but for this purpose, one will suffice. XD

这就是说,我相信剧本是自上而下运行的,所以只有最后一次提到应该坚持下去。您可以在某些情况下利用这一点,但对于此目的,一个就足够了。XD



Be careful when you decide to fiddle with the PATH in .zshrc: Since the file is processed by every interactive subshell, the PATH would get longer and longer for each subshell, with the same directory occuring in it several times. This can become a nightmare if you later try to hunt down PATH-related errors.

在决定修改.zshc中的路径时要小心:因为每个交互的子外壳都会处理该文件,所以每个子外壳的路径会越来越长,同一个目录会在其中出现多次。如果您稍后尝试查找与路径相关的错误,这可能会成为一场噩梦。


Since you are using zsh, you can take advantage that the scalar variable PATH is mirrored in the array variable path, and that you can ask zsh to keep entries in arrays unique.

由于您使用的是zsh,因此可以利用标量变量PATH在数组变量path中的镜像,并且可以要求zsh保持数组中的条目唯一。


Hence, the first thing I would do is put a

因此,我要做的第一件事是将一个


 typeset -aU path

in your .zshrc; this (due to mirroring) also keeps the entries in PATH unique. You can put this statement anywhere, but I have it for easier maintenance before my first assignment to PATH or path.

在您的.zshc中;这(由于镜像)也使路径中的条目保持唯一。您可以将此语句放在任何位置,但我保留它是为了在我第一次将其分配给PATH或PATH之前更容易维护。


It is up to you to decide where exactly you add a new entry to PATH or path. The entries are searched in that order which is listed in the variable. You have to ask yourself two questions:

将新条目添加到路径或路径的确切位置由您决定。按照变量中列出的顺序搜索条目。你必须问自己两个问题:



  • Are some directories located on a network share, where you can sometimes expect access delays (due to bad network conditions)? Those directories should better show up near the end of the path.

    某些目录是否位于网络共享上,有时可能会出现访问延迟(由于网络状况不佳)?这些目录最好显示在路径末尾附近。



  • Do you have commands which occur in more than one directoryin your path? In this case, a path search will always find the first occurance only.

    在您的路径中,是否有出现在多个目录中的命令?在这种情况下,路径搜索将始终只找到第一个匹配项。




Finally, don't forget that your changes will be seen after zsh processes the file. Therefore, you could create a new subshell after having edited the file, or source .zshrc manually.

最后,不要忘记,您的更改将在zsh处理文件后看到。因此,您可以在编辑完文件后创建新的子外壳,或手动创建.zshc源文件。


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com