目录

理解zsh以及他的配置文件

zsh是什么

理解shell是什么

根据shell在wiki中的定义

In computing, a shell is a computer program which exposes an operating system’s services to a human user or other programs.

shell即为一个计算机程序,用于提供操作系统的服务给人或者其他程序。可以理解为一个操作系统的接口,用于提供给用户或者其他程序的接口。 就跟我们开发的程序一样,我们可以使用shell的接口来调用操作系统的功能。

交互式shell和非交互式shell

交互式shell被定义为简单地将命令作为用户在终端上的输入,并将输出确认给用户的shell。例如,我们在进行ssh登录时, shell会等待我们输入用户信息,并且会在输入完成,返回登录结果。这就是一个交互式shell。

非交互式shell被定义为不等待用户输入,而是直接执行命令,通常我们在执行自动化操作,比如执行某个定时任务,这通常就是一个非交互shell。

zsh跟shell有什么关系

zsh是一个shell,它是一个终端程序,它可以让我们在终端上使用shell的功能。我们可以在mac的终端上输入 cat /etc/shells, 可以发现mac系统自带的shell有很多 https://qiniu.ckfear.cn/blog/8655390.png mac系统在之前的版本中使用的就是bash,现在的mac系统默认使用的是zsh,因为zsh之前的配置过于复杂,很难推广,随着 ohmyzsh的出现,配置也可以一键生成,只需要修改.zshrc就可以完成原本很复杂的配置。

zsh的配置文件

zsh默认会在我们的用户目录下创建一个.zshrc文件,除此之外,zsh也有其他的配置文件。

.zshenv

.zshenv is always sourced. It often contains exported variables that should be available to other programs. For example, $PATH, $EDITOR, and $PAGER are often set in .zshenv. Also, you can set $ZDOTDIR in .zshenv to specify an alternative location for the rest of your zsh configuration.

zshenv通过设置环境变量来让zsh更加灵活,比如设置$PATH,$EDITOR,$PAGER等等。也可以设置$ZDOTDIR来指定zsh的配置文件的位置。

.zprofile

.zprofile is for login shells. It is basically the same as .zlogin except that it’s sourced before .zshrc whereas .zlogin is sourced after .zshrc. According to the zsh documentation, “.zprofile is meant as an alternative to .zlogin for ksh fans; the two are not intended to be used together, although this could certainly be done if desired.”

和.zlogin基本一样,只是调用顺序不一样,.zprofile会在.zshrc之前调用,而.zlogin会在.zshrc之后调用。设计的目的主要是为了ksh的使用者。他和.zlogin最好不要一起使用。

.zshrc

.zshrc is for interactive shells. You set options for the interactive shell there with the setopt and unsetopt commands. You can also load shell modules, set your history options, change your prompt, set up zle and completion, et cetera. You also set any variables that are only used in the interactive shell (e.g. $LS_COLORS).

zshrc是为了交互式shell的。我们可以通过它去配置我们交互式shell的选项,比如加载某些shell模块、设置历史记录、更改提示符、设置zle和补全等等。 我们也可以设置一些只在交互式shell中使用的变量。

.zlogin

.zlogin is for login shells. It is sourced on the start of a login shell but after .zshrc, if the shell is also interactive. This file is often used to start X using startx. Some systems start X on boot, so this file is not always very useful.

和zprofile作用基本一样,通常只会在登录之后加载一次,有的系统会在开机时自动加载,所以这个文件不是很有用。

.zlogout

.zlogout is sometimes used to clear and reset the terminal. It is called when exiting, not when opening.

顾名思义,.zlogout通常用来清除和重置终端。当退出时调用,而不是打开时调用。

加载顺序

这些配置文件也是有加载顺序的

.zshenv → [.zprofile if login] → [.zshrc if interactive] → [.zlogin if login] → [.zlogout sometimes]

参考

https://unix.stackexchange.com/a/71258