$_ CLI In Depth
Learning progress 0%
lesson environment path dotfiles

Startup Files: .bash_profile, .bashrc & .profile

5 min read Module 9: Environment, PATH & Dotfiles Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:11

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 kindFiles read
Login, interactive/etc/profile, then the first user profile of .bash_profile, .bash_login, or .profile
Non-login, interactive~/.bashrc
Non-interactiveThe 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.

BASH1 lines
if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc"; fi

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

Tip

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.

BASH2 lines
printf 'DEMO_GREETING=from-rc\n' > demo.bashrc
bash --rcfile demo.bashrc -i -c 'echo "$DEMO_GREETING"' 2>/dev/null < /dev/null

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

Which startup files bash reads

Visualizer

Bash reads different files for login shells, for interactive shells that are not login shells, and for scripts.

1 / 4
$ bash -l
Files read, in order
1/etc/profile
2~/.bash_profile (first of the next three that exists)
3~/.bash_login
4~/.profile

A login shell is started by login, by ssh, or with bash -l. Bash reads a fixed list of files for this case.

bash -l -c 'shopt -q login_shell && echo "login shell: yes"'
Terminal lab · 2 steps · real Linux in your browser

Startup files and --rcfile

Test a startup file with --rcfile, then build a login-profile chain in a throwaway home directory.

01
Test a startup file with --rcfile
Create rc-test.sh in /workspace. It writes a small rc file named demo.bashrc, then starts an interactive shell that reads only that file and prints one variable: ``bash #!/bin/bash cat > /workspace/demo.bashrc <<'RC' DEMO_GREETING=from-rc RC bash --rcfile /workspace/demo.bashrc -i -c 'echo "$DEMO_GREETING"' 2>/dev/null </dev/null ` Run bash rc-test.sh. The output is from-rc`.
Show hint for step 1
--rcfile replaces ~/.bashrc for that interactive shell only, so your real startup files stay untouched.
02
Chain the login profile to bashrc
Create profile-chain.sh in /workspace. It builds a fake home directory in /workspace/fakehome with a .bash_profile that loads a .bashrc, then starts a login shell with a clean environment that uses that home: ``bash #!/bin/bash mkdir -p /workspace/fakehome printf 'FROM_RC=rc\n' > /workspace/fakehome/.bashrc printf 'FROM_PROFILE=profile\n. "$HOME/.bashrc"\n' > /workspace/fakehome/.bash_profile env -i HOME=/workspace/fakehome PATH=/usr/bin:/bin:/usr/local/bin bash -l -c 'echo "$FROM_PROFILE $FROM_RC"' 2>/dev/null </dev/null ` Run bash profile-chain.sh. The output line is profile rc`, which means the login profile ran and loaded the bashrc.
Show hint for step 2
A login shell reads .bash_profile, and the dot command runs .bashrc in that same shell.
Check yourself Question 1 of 4
Concept Check User profile file for login recall

For an interactive login shell, which user file does bash read?

Recap & Key Takeaways

A login shell reads one user profile file, and a non-login interactive shell reads only ~/.bashrc.
A login file that sources ~/.bashrc gives both kinds of shell the same settings.
Put exports and PATH in the login path, and put aliases, functions, and the prompt in ~/.bashrc.
The --rcfile option lets you test a startup file without changing your real home directory.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue