简体   繁体   English

如何在ZSH中使PATH更简洁?

[英]How can I make my PATH more succinct in ZSH?

I am using ZSH. 我正在使用ZSH。 I was having trouble running the Anaconda package manager commands in my terminal. 我在终端中运行Anaconda软件包管理器命令时遇到麻烦。 I found that I could add the bin to my PATH using this code: 我发现可以使用以下代码将垃圾箱添加到PATH中:

export PATH="$HOME/anaconda3/bin:$PATH"

That worked. 那行得通。 All of the Anaconda and associated commands work and it left me with this PATH: 所有的Anaconda和关联的命令都可以正常工作,这让我有了以下PATH:

/Users/USER/anaconda3/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin

But the PATH variable would reset each time I closed the terminal window. 但是,每次关闭终端窗口时,PATH变量都会重置。 After some research, I figured out how modify my .zshrc to make it permanent. 经过研究,我弄清楚了如何修改.zshrc以使其永久。 I simply pasted this into my .zshrc: 我只是将其粘贴到我的.zshrc中:

export PATH=/Users/USER/anaconda3/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin

All the commands work and are permanent, but aren't I setting myself up for failure down the road? 所有命令都有效并且是永久性的,但是我不是要为失败做好准备吗? Everything in the PATH, minus the "/Users/USER/anaconda3/bin" were set automatically. 减去“ / Users / USER / anaconda3 / bin”中的所有内容都会自动设置。 What happens when I update Python, for example? 例如,当我更新Python时会发生什么? How can I include the "USER/anaconda3/bin" portion of my PATH in my .zshrc without being so explicit? 如何在我的.zshrc中包含PATH的“ USER / anaconda3 / bin”部分而又没有那么明确?

Your PATH concern is not zsh specific. 您对PATH的关注并非特定于zsh You could organize things differently. 您可以以不同的方式组织事情。

For example, you could decide to add a directory $HOME/bin/ early in your PATH and put inside that $HOME/bin/ symbolic links to the executables (or scripts ) that you want to use. 例如,您可以决定在PATH早期添加一个目录$HOME/bin/ ,并将该$HOME/bin/符号链接放入要使用的可执行文件 (或脚本 )中。 BTW, I recommend to have a short PATH (containing some $HOME/bin/ etc...) since it is more efficient and less messy to understand. 顺便说一句,我建议使用短PATH (包含一些$HOME/bin/等...),因为它效率更高,理解起来也不那么混乱。

What happens when I update Python, for example? 例如,当我更新Python时会发生什么?

Let's suppose that Python is installed in your system in /usr/bin/python (and that you use some Linux distribution -or some other Unix- with a good package manager dealing with that;on MacOSX consider homebrew ). 让我们假设Python安装在/usr/bin/python中的系统中(并且您使用一些Linux发行版(或其他一些Unix),并且具有良好的软件包管理器 ;在MacOSX上考虑使用homebrew )。 When that file /usr/bin/python is updated, any future exec of it (eg by some shell) will use the new version. 当该文件/usr/bin/python更新时,它的任何将来的执行程序(例如,通过某些shell)将使用新版本。 Read carefully execve(2) . 仔细阅读execve(2)

If ou have several Python-s and the one you want to use is under /Library/Frameworks/Python.framework/Versions/3.6/bin and you upgraded it to some Python 3.7 installed under /Library/Frameworks/Python.framework/Versions/3.7/bin you would need to change your PATH . 如果ou有多个Python-s,而您要使用的是/Library/Frameworks/Python.framework/Versions/3.6/bin 3.6 /Library/Frameworks/Python.framework/Versions/3.6/bin ,则将其升级到安装在/Library/Frameworks/Python.framework/Versions/3.7/bin下的Python 3.7。 /Library/Frameworks/Python.framework/Versions/3.7/bin您需要更改PATH

If you followed my suggestion, you would (for example) just have a symlink from $HOME/bin/python to /Library/Frameworks/Python.framework/Versions/3.6/bin/python and you would upgrade that symlink when installing Python3.7 using for example: 如果遵循我的建议,(例如)您将只有一个从$HOME/bin/python/Library/Frameworks/Python.framework/Versions/3.6/bin/python符号链接,并且在安装Python3时可以升级该符号链接。 7.使用举例:

# remove the old symlink
rm -v $HOME/bin/python
# add the new one
ln -sv /Library/Frameworks/Python.framework/Versions/3.7/bin/python \
   $HOME/bin/python

In all cases, you are responsible of having a good enough PATH (and preferably a short one). 在所有情况下,您都有责任拥有足够好的PATH (最好是较短的PATH )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM