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

If you have ever put something in a file like .bashrc and had it not work, or are confused by why there are so many different files — .bashrc, .bash_profile, .bash_login, .profile etc. — and what they do, this is for you.

The issue is that Bash sources from a different file based on what kind of shell it thinks it is in. For an “interactive non-login shell”, it reads .bashrc, but for an “interactive login shell” it reads from the first of .bash_profile, .bash_login and .profile (only). There is no sane reason why this should be so; it’s just historical. Follows in more detail.

For Bash, they work as follows. Read down the appropriate column. Executes A, then B, then C, etc. The B1, B2, B3 means it executes only the first of those files found.

From http://www.solipsys.co.uk/new/BashInitialisationFiles.html
The bash executable
The systemwide initialization file, executed for login shells
The personal initialization file, executed for login shells
The individual per-interactive-shell startup file
The individual login shell cleanup file, executed when a login shell exits
Individual readline initialization file

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. (Starting a new window in OS X’s Terminal.app seems to count as a login shell, though.)

So typically (or sooner or later), what you will encounter are non-login shells. So this case is what you should write your config files for. This means putting most of your stuff in ~/.bashrc, having exactly one of ~/.bash_profile, ~/.bash_login, and ~/.profile, and sourcing ~/.bashrc from it. If you have nothing that you specifically want to happen only for login shells, you can even symlink one of the three to ~/.bashrc.

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

For bash, put stuff in ~/.bashrc, and make ~/.bash_profile source it.
For zsh, put stuff in ~/.zshrc, which is always executed.

Original post :https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

I am enthusiastic web developer, with reasonable knowledge in HTML5 Game development, Javascript and Web Threat

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store