The order for each kind of shell
For an interactive login shell, bash reads /etc/profile first, then the first of ~/.bash_profile, ~/.bash_login, or ~/.profile that exists. Bash reads only one of the three user files. When the login shell exits, it reads ~/.bash_logout. For an interactive shell that is not a login shell, bash reads ~/.bashrc and nothing else. For a non-interactive shell, bash reads only the file named by BASH_ENV, if that variable is set.
| Shell kind | Files read |
|---|---|
| Login, interactive | /etc/profile, then the first user profile of .bash_profile, .bash_login, or .profile |
| Non-login, interactive | ~/.bashrc |
| Non-interactive | The file named by BASH_ENV, if set |
The table makes one fact clear. A new terminal tab that is not a login shell does not read ~/.bash_profile. A setting placed only there will not appear in that tab, and this is one of the most common reasons for a missing variable.
The common pattern
Many people solve this by making the login file load the interactive file. The login file sets things that should happen once per session, such as PATH and exported variables. The interactive file sets things that only make sense at a prompt, such as aliases, functions, and the prompt string. The login file then sources the interactive one, so both paths get the same settings.
if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc"; fiThe test uses -f, so a missing file is not an error. The dot command runs the file in the current shell, which is the only way for its variables and aliases to stay after it finishes. Running it as a separate program with bash ~/.bashrc would throw away every change.
Which file gets which setting
A simple split keeps the files easy to reason about. Put exports that every program needs, and PATH changes, in the login file. Put aliases, functions, shell options, and the prompt in ~/.bashrc. Keep ~/.profile for settings that other shells, such as sh or a graphical session, also need. Avoid printing output from either file, because some tools such as scp and rsync break when a login shell writes text to standard output before they start.
Test startup files with a fresh environment. env -i starts a program with no inherited variables, so the result depends only on the files you name.
Testing a file without your real home
The same idea works for the login path. Set HOME to a throwaway directory and start a login shell with a clean environment, so that the profile files in that directory are the only ones read. This is the safest way to test a change to .bash_profile without risking your real session, and it works in the lab just as it does on a laptop.
The --rcfile option makes an interactive shell read a file of your choice instead of ~/.bashrc. Combined with -i and -c, you can test a file and print the result in one command. Pass 2>/dev/null and < /dev/null so that job control warnings and terminal prompts do not clutter the output.
printf 'DEMO_GREETING=from-rc\n' > demo.bashrc
bash --rcfile demo.bashrc -i -c 'echo "$DEMO_GREETING"' 2>/dev/null < /dev/nullThe output is from-rc. The variable exists only because the file was read by the interactive shell. The same check works for any file, which makes it a quick way to verify changes before you put them in your real home directory.
A minimal working setup
A small, reliable setup uses two files with a few lines each. The login file loads the interactive file, sets the variables that every program needs, and does little else. The interactive file defines aliases, functions, shell options, and the prompt. Keeping both short makes them readable on one screen, which is the best protection against a file that quietly breaks every new session.
Some lines belong at the very top of the interactive file, before anything else. A check that stops the file when the shell is not interactive is the classic example. Without it, a script that sources the file can print prompt text or change options it did not ask for. A single line at the top keeps the file safe for scripts and remote commands.
Order inside a file matters. Bash reads the file from top to bottom, so a line that depends on a later line fails the first time. If the interactive file runs a tool that is found through PATH, the PATH line must come before the command that uses it, and an alias that refers to a function must come after the function is defined.
Keep a copy of the last working version before a large edit. When something breaks, copy the saved file back, open a new shell, and then compare the two versions line by line to find the change that caused the problem. A short history of working files is cheaper than a long search through a broken one.
Test each change in a new shell. Opening a fresh terminal tab, or starting a new login shell, shows whether the settings load from the beginning. This test is cheap, and it catches the most common mistake, which is a change that works only in the shell where you typed it by hand.
When a file is read twice
A common problem is a file that runs twice, once from the login path and once from the interactive path. The effect is usually harmless for exports, but it is a problem for PATH, where each pass adds the same directory again. Guard PATH changes so that they run only once, or keep the PATH change in the login file alone and let the interactive file leave PATH untouched.