Linux 和 MacOS 中的 profile 文件详解(二)

上篇文章讲解了 profile 文件的作用、login shell 和 non-login shell 的定义、不同 profile 被 bash shell 在不同情况下的加载顺序和作用,本文讲解一下 zsh shell 相关的知识。

zsh shell

 MacOS 从 Catalina 版本开始将 zsh 作为默认登录 shell 和交互式 shell。

zsh 的配置文件加载顺序大致如下:

  1. /etc/zshenv:这是zsh启动时的第一个配置文件,全局生效,对所有用户都有效。
  2. ~/.zshenv:这是针对每个用户的第一个配置文件,对单个用户有效。
  3. /etc/zsh/zshrc:这是zsh的默认配置文件,对所有用户有效。
  4. ~/.zshrc:这是每个用户的默认配置文件,对单个用户有效。
  5. ~/.zprofile、~/.zsh、~/.zlogin、~/.zlogout:这些文件是针对每个用户的登录和退出脚本,对单个用户有效。

zsh shell 是不会执行 /etc/profile 和 ~/.bash_profile 文件的,当从 bash 切换为 zsh 时,如果不想再配置一遍 ~/.zshrc 文件,可以在 ~/.zshrc 文件中加上 source ~/.bash_profile,以直接从 ~/.bash_profile 文件读取配置。

如何查看当前使用的 shell?

可以使用如下命令来查看当前使用的 shell:

echo $SHELL

在我电脑上运行效果如下:

$ echo $SHELL    
/bin/zsh

可以使用如下命令查看系统中安装了哪些 shell:

cat /etc/shells

在我电脑上运行效果如下:

cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

你可能感兴趣的:(Linux系列知识讲解,linux,macos,后端,运维,命令行,bash)