The settings .bash_profile
and .bashrc
have both similar roles and certain differences. To understand this, we note that a shell can be either interactive or non-interactive, and that it can be either a login shell or a non-login shell.
In an interactive shell, the user may type commands into it, execute them, and see the output. A non-interactive shell doesn’t allow so. When we enter the terminal app in Mac or Ubuntu, we are using an interactive shell. When we run a shell script, however, a non-interactive shell is started to run it until the script finishes.
A login shell requires the user to type username and password to log in, but a non-login shell does not. Therefore a login shell must be interactive. Certainly, the shell explicitly created by logging in, in a terminal app instance, is a login shell. Since the terminal app automatically logs in when starting up, it is a login shell. Meanwhile, non-login shells include the new Bash instance the user invokes by calling /bin/bash
, or another kind of shell, like Zsh.
Now, an interactive login shell reads Bash profile, while an interactive non-login shell reads Bashrc. Default paths of Bash profile and Bashrc are both the user’s home. On the other hand, a non-interactive shell reads neither of them.
Actually there are /etc/profile
, /etc/bash.bashrc
, BASH_ENV
, ~/.bash_login
, ~/.bash_logout
, and ~/.profile
, each with subtle differences due to historical reasons. To me, the most reliable solution is that we put all settings into Bashrc, and require Bash profile to source Bashrc.
❧ January 19, 2017