Jan Fan     About     Archive     Feed     中文博客

Shell Setup Files Loading (Bash & Zsh)

Login vs. Non-Login Terminals

A login or non-login shell may load different setup files when they are created (a windows or terminal you open). For details, ref.

OS X

In OS X, ALL interactive bash shells are login shells.

Linux

On Linux, the situation is typically reversed: bash shells created interactively are NON-login shells.

Setup Files

Setup files determine the alias, environments variables and others stuff when you start to work on your shell. Configuring and Loading them correctly will save you tons of efforts.

Different shells have different setup files. Besides, there are multiple setup files, for every specific shell.

!--more--

Bash

For Bash Shell, the setup files are loaded in the order : A -> B -> C ...

When there are multiple files in the same alphabetical level, like B1, B2, B3 and so on, once one files will be loaded, on the priority: B1 > B2 > B3.

+----------------+-----------+-----------+------+
|                |Interactive|Interactive|Script|
|                |login      |non-login  |      |
+----------------+-----------+-----------+------+
|/etc/profile    |   A       |           |      |
+----------------+-----------+-----------+------+
|/etc/bash.bashrc|           |    A      |      |
+----------------+-----------+-----------+------+
|~/.bashrc       |           |    B      |      |
+----------------+-----------+-----------+------+
|~/.bash_profile |   B1      |           |      |
+----------------+-----------+-----------+------+
|~/.bash_login   |   B2      |           |      |
+----------------+-----------+-----------+------+
|~/.profile      |   B3      |           |      |
+----------------+-----------+-----------+------+
|BASH_ENV        |           |           |  A   |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|~/.bash_logout  |    C      |           |      |
+----------------+-----------+-----------+------+

Zsh

Same rule applied on Zsh shell as above, with different setup files

If you have never heard and tried Zsh ever, I strongly suggest you to read the slides about why-zsh-is-cooler-than-your-shell, which will open a new world for you.

+----------------+-----------+-----------+------+
|                |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      |           |      |
+----------------+-----------+-----------+------+

Reference

[1] How to load ~/.bash_profile when entering bash from within zsh?

[2] Zsh/Bash startup files loading order (.bashrc, .zshrc etc.)

[3] why-zsh-is-cooler-than-your-shell

Comments

comments powered by Disqus