"Go" the correct setting of the PATH environment variable under ZSH in Mac OS X

Source: Internet
Author: User

Using zsh in Mac OS X, the environment variable path will become very chaotic, showing that the path you set is always placed after the system path, and some paths will be duplicated. This is because we don't quite understand the order in which the files are loaded at startup zsh and the shell type of Mac OS X.

The following files are read sequentially during the zsh startup process:

    1. /etc/zshenv
    2. $ZDOTDIR/.zshenv (default is $HOME when $ZDOTDIR is not set)
    3. If it is login shell, read/etc/zprofile, $ZDOTDIR/.zprofile
    4. If it is interactive shell, read/ETC/ZSHRC, $ZDOTDIR/.ZSHRC
    5. If it is login shell, read/etc/zlogin, $ZDOTDIR/.zlogin

Login shell is the user login, enter the user name and password after the launch of the Shell,non-login shell is logged in after the open shell. The interactive shell executes on the terminal, the shell waits for your input, and immediately executes the command you submit, interacting with the user, and the non-interactive Shell executes in shell script (non-interactive) mode.

Interactive, login shell is more common, we ssh to the remote host character terminal, is typical of such shell. Non-interactive, login shell is rarely seen, some X settings will let you log in, only to load its corresponding settings file. Interactive, the Non-login shell typically occurs when a new shell is launched through an existing session, such as a shell that starts in a screen under Linux, and a tmux,linux under a desktop environment, such as a shell such as g Nome Shell. Non-interactive, the Non-login shell is common, and we execute shell script as this kind of shell.

1234< Span class= "Line-number" >5678< Span class= "Line-number" >9101112< Span class= "Line-number" >13141516 171819 20212223 242526 2728             
+--------------+-------------+-------------+-----------------+-----------------+|    |  Login |      Non-login |    Login | Non-login || | Interactive | Interactive | non-interactive | non-interactive |+--------------+-------------+-------------+-----------------+-----------------+|/etc/zshenv | A | A | A | A |+--------------+-------------+-------------+-----------------+-----------------+|~/.zshenv | B | B | B | B |+--------------+-------------+-------------+-----------------+-----------------+|/etc/zprofile | C | | C | |+--------------+-------------+-------------+-----------------+-----------------+|~/.zprofile | D | | D | |+--------------+-------------+-------------+-----------------+-----------------+|/ETC/ZSHRC | E | C | | |+--------------+-------------+-------------+-----------------+-----------------+|~/.ZSHRC | F | D | | |+--------------+-------------+-------------+-----------------+-----------------+|/etc/zlogin | G | | E | | +--------------+-------------+-------------+-----------------+-----------------+ |~/.zlogin | H | | F | | +--------------+-------------+-------------+-----------------+-----------------+ | | | | | | +--------------+-------------+-------------+-----------------+-----------------+ | | | | | | +--------------+-------------+-------------+-----------------+-----------------+ |~/.zlogout | I | | G | | +--------------+-------------+-------------+-----------------+-----------------+ |/etc/zlogout | J | | H | | +--------------+-------------+-------------+-----------------+-----------------+    

So the question is, what type of shell is open in Mac OS X Iterm.app or Terminal.app startup? Generally speaking, it should be interactive, non-login shell, but in fact it is interactive, login shell, as to why this does not dig into the. The following test code can prove:

12
[[ -o login ]] && echo ‘yes‘ || echo ‘no‘[[ -o interactive ]] && echo ‘yes‘ || echo ‘no‘

Therefore, the shell that opens Iterm.app or Terminal.app will read all the files that exist in the above 1-5, and if multiple files are set on the PATH environment variable, the resulting PATH environment variable will be more complex. It's no surprise that part of the path repeats. Looking at/etc/zshenv, you will see that the call is /usr/libexec/path_helper , and it is loading the system path, and putting the system path to the front. If the next user in the $ZDOTDIR in the file load their own set of paths and put the first, then the next load of/etc/zprofile,/ETC/ZSHRC may also be called /usr/libexec/path_helper , causing the system path back to the front, creating a wonderful path The environment variable system path, the self-set path, the system path interleaving phenomenon.

Having learned so much, the solution is simple enough that only the necessary files are involved in the PATH environment variable in the 1-5 above. For example, in/etc/zshenv the /usr/libexec/path_helper system path is set by calling, the self-set path is placed at the top of the $ZDOTDIR/.zshenv, and the rest of the files do not involve the PATH environment variable setting.

Original address: https://www.jmlog.com/set-path-in-zsh-on-mac-os-x/

"Go" the correct setting of the PATH environment variable under ZSH in Mac OS X

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.