【Linux】bash 和 zsh 的启动脚本(.zshrc,.bash_profile,~/.profile 等区别)

Posted by 西维蜀黍 on 2020-06-13, Last Modified on 2021-09-21

结论

zsh

当默认 shell 为 zsh 时,且当开启一个新 tab 时(或者一台主机通过 SSH 登录本主机时),/etc/zprofile/etc/zshrc~/.zshrc 均会被执行 ,且执行顺序为:

  • /etc/zprofile
  • /etc/zshrc
  • ~/.zshrc

当开启一个新 tab 时,

Last login: Sat Jun 13 11:46:23 on ttys006
execute /etc/zprofile
execute /etc/zshrc
execute ~/.zshrc

当一台主机通过 SSH 登录本主机时

$ ssh wei.shi@192.168.16.173
Last login: Sat Jun 13 11:54:43 2020
execute /etc/zprofile
execute /etc/zshrc
execute ~/.zshrc
~ $

当启用一个新zsh 进程时

$ zsh
execute /etc/zshrc
execute ~/.zshrc
$

bash

Interactive Login Shell

这种情况包括

  • 在本机新开启一个新 shell tab
  • 一台主机通过 SSH 登录本主机

The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports anerror. Tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section.

When bash is invoked as an interactive login shell, or as a non-interactive shell with the –login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The –noprofile option may be used when the shell is started to inhibit this behavior.

当默认 shell 为 bash 时,且当开启一个新 tab 时(或者一台主机通过 SSH 登录本主机时),以下文件的内容会有意义,且执行顺序为:

  • /etc/bashrc
  • /etc/profile - 全局profile文件
  • 以下三个文件,三选一执行(只会执行最多一个文件),执行顺序从高到低
    • ~/.bash_profile
    • ~/.bash_login
    • ~/.profile

Example

当开启一个新 tab 时(或者一台主机通过 SSH 登录本主机时),

Last login: Sat Jun 13 12:06:35 from 192.168.16.227
execute /etc/bashrc
execute /etc/profile
execute ~/.bash_profile

当一台主机通过 SSH 登录本主机时

ssh wei.shi@192.168.16.173
Last login: Sat Jun 13 12:04:54 2020
execute /etc/bashrc
execute /etc/profile
execute ~/.bash_profile

Interactive Shell Without Login Shell

这种情况包括

  • 当启动一个新 bash 进程

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.

当启动一个新 bash 进程时,以下文件会被执行

  • ~/.bashrc

Example

当启动一个新 bash 进程时

$ bash
execute ~/.bashrc
bash-3.2$

如何算作 Login Shell

Typically, most users will encounter a login shell only if either:

  • they logged in from a tty, not through a GUI
  • they logged in remotely, such as through ssh.
  • If the shell was started any other way, such as through GNOME’s gnome-terminal or KDE’s konsole, then it is typically not a login shell — the login shell was what started GNOME or KDE behind your back when you logged in; things started anew are not login shells.
  • New terminals or new screen windows you open are not login shells either. (However, starting a new window in OS X’s Terminal.app or iTerm2 counts as a login shell)

使用 zsh

# 设置默认 shell 为 zsh
$ chsh -s /bin/zsh
  • /etc/zprofile
  • /etc/zshrc
  • ~/.zshrc

For zsh: [Note that zsh seems to read ~/.profile as well, if ~/.zshrc is not present.]

+----------------+-----------+-----------+------+
|                |Interactive|Interactive|Script|
|                |login      |non-login  |      |
+----------------+-----------+-----------+------+
|/etc/zshenv     |    A      |    A      |  A   |
+----------------+-----------+-----------+------+
|~/.zshenv       |    B      |    B      |  B   |
+----------------+-----------+-----------+------+
|/etc/zprofile   |    C      |           |      |
+----------------+-----------+-----------+------+
|~/.zprofile     |    D      |           |      |
+----------------+-----------+-----------+------+
|/etc/zshrc      |    E      |    C      |      |
+----------------+-----------+-----------+------+
|~/.zshrc        |    F      |    D      |      |
+----------------+-----------+-----------+------+
|/etc/zlogin     |    G      |           |      |
+----------------+-----------+-----------+------+
|~/.zlogin       |    H      |           |      |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|~/.zlogout      |    I      |           |      |
+----------------+-----------+-----------+------+
|/etc/zlogout    |    J      |           |      |
+----------------+-----------+-----------+------+

实验

当开启一个新 tab 时,

Last login: Sat Jun 13 11:46:23 on ttys006
execute /etc/zprofile
execute /etc/zshrc
execute ~/.zshrc

当一台主机通过 SSH 登录本主机时

$ ssh wei.shi@192.168.16.173
Last login: Sat Jun 13 11:54:43 2020
execute /etc/zprofile
execute /etc/zshrc
execute ~/.zshrc
~ $

当启用一个新zsh 进程时

$ zsh
execute /etc/zshrc
execute ~/.zshrc
$

结论:

当开启一个新 tab 时(或者一台主机通过 SSH 登录本主机时)/etc/zprofile/etc/zshrc~/.zshrc 均会被执行 ,且执行顺序为:

  • /etc/zprofile
  • /etc/zshrc
  • ~/.zshrc

使用 bash

# 设置默认 shell 为 bash
$ chsh -s /bin/bash
  • /etc/bashrc
  • /etc/profile - 全局初始化,当新建一个 Shell tab ,或者每次 SSH 到远程主机,或者等次登录 Shell 时,一定会被执行(The systemwide initialization file, executed for login shells)
  • 当新建一个 Shell tab ,或者每次 SSH 到本主机,或者每次登录 Shell 时(The systemwide initialization file, executed for login shells)以下三个文件,三选一执行(搜索顺序如下所示,当搜索到之后,下一个文件就不会被执行了)
    • ~/.bash_profile
    • ~/.bash_login
    • ~/.profile
  • ~/.bashrc (当新建一个 Shell tab ,或者每次 SSH 到远程主机,或者等次登录 Shell 时,该文件不会被执行)

/home/username/.profile/home/username/.bash_profile 是针对特定用户的。也就是说,当当前登录身份为这个用户时,这个用户中 ~/.profile~/.bash_profile 文件会被执行。

/etc/profile/etc/bashrc 是全局设置(并不针对特定用户),这意味着:不管使用任何一个用户登录,/etc/profile/etc/bashrc 中的文件内容都会被执行。

实验

当开启一个新 tab 时(或者一台主机通过 SSH 登录本主机时),

Last login: Sat Jun 13 12:06:35 from 192.168.16.227
execute /etc/bashrc
execute /etc/profile
execute ~/.bash_profile

当一台主机通过 SSH 登录本主机时

ssh wei.shi@192.168.16.173
Last login: Sat Jun 13 12:04:54 2020
execute /etc/bashrc
execute /etc/profile
execute ~/.bash_profile

当启用一个新 bash 进程时

$ bash
execute ~/.bashrc
bash-3.2$

结论:

当开启一个新 tab 时(或者一台主机通过 SSH 登录本主机时)/etc/bashrc/etc/profile~/.bash_profile 均会被执行 ,且执行顺序为:

  • /etc/bashrc
  • /etc/profile - 全局初始化,当新建一个 Shell tab ,或者每次 SSH 到远程主机,或者等次登录 Shell 时,会被执行(The systemwide initialization file, executed for login shells)
  • 当新建一个 Shell tab ,或者每次 SSH 到本主机,或者每次登录 Shell 时(The systemwide initialization file, executed for login shells)以下三个文件,三选一执行(只会执行最多一个文件),执行顺序从高到低
    • ~/.bash_profile
    • ~/.bash_login
    • ~/.profile
  • ~/.bashrc(当新建一个 Shell tab ,或者每次 SSH 到远程主机,或者等次登录 Shell 时,该文件不会被执行)

Reference