December 4, 2026

.bashrc vs .bash_profile: Which File Bash Reads, When, and Where PATH Belongs

A row of recipe cards on a cork board with one card lit by a beam of light

An alias added to .bash_profile works in one terminal and not in another. A PATH change added to .bashrc shows up twice. A script run from cron cannot find a program that works fine by hand. All three come from the same source: bash reads different startup files depending on how it was started, and it reads none of them for a plain script. To pin down exactly which file loads when, each startup file in a test home directory was made to print its own name, and bash 5.2.21 was started every way that matters. The output below is what it printed.

Video: Zach Gollwitzer on YouTube

The test

One line per file

Each of .bash_profile, .bash_login, .profile, .bashrc and .bash_logout contained a single line, echo "read <filename>". Bash was then started with a clean environment, so nothing from the real system could interfere, in each of the modes below.

How bash was started Files read
Login shell (bash -l -c true) .bash_profile
Interactive, not login (bash -i -c true) .bashrc
Script or command (bash -c true) none
Interactive login (bash -l -i), then exit .bash_profile, then .bash_logout
Login, with no .bash_profile .bash_login
Login, with only .profile .profile

The fourth row surprises people: an interactive login shell read .bash_profile and did not read .bashrc at all.

Corridor with doors

Login shells

The first file found, and only that one

The GNU Bash manual describes a login shell’s behavior exactly as the test showed it. After reading /etc/profile, bash looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads the first one that exists and is readable. Only one of the three runs. With all three present, only .bash_profile printed. Removing it promoted .bash_login; removing that promoted .profile.

That ordering is the source of a classic mistake. Many distributions ship a .profile with useful defaults. Creating a new, empty .bash_profile to add one line silently stops .profile from running at all, and everything it used to set up disappears from login shells.

What counts as a login shell

A shell started when someone logs in: a console login, an SSH session that opens a shell, or anything run with bash -l or bash --login. On logout, bash also reads .bash_logout, which the test confirmed after exit.

Interactive non-login shells

.bashrc

The manual says that when bash runs as an interactive shell that is not a login shell, it reads and executes commands from ~/.bashrc, if that file exists. This is the shell most terminal windows on a Linux desktop open, and every new shell typed inside an existing one.

The line that ties them together

Because a login shell never reads .bashrc on its own, the usual arrangement makes it do so. The manual gives the line it calls typical for ~/.bash_profile:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

Ubuntu’s default .profile in /etc/skel contains the same idea: lines 13 to 15 check for $HOME/.bashrc and source it. That is why, on a stock Ubuntu account, aliases in .bashrc work in SSH sessions too, and why they stop working the day someone creates a .bash_profile without that line. New accounts get these files copied from /etc/skel when they are created with a home directory, as covered in the post on Linux users and groups.

Timer

Scripts read nothing

Non-interactive shells

bash -c true printed nothing: no startup file at all. A script is a non-interactive, non-login shell, and the manual says that in that case bash instead checks for a variable named BASH_ENV and, if it is set, uses the expanded value as the name of a file to read and execute. With BASH_ENV pointing at a test file, the same command printed read env.sh.

This is the cron problem. A program added to PATH in .bashrc is invisible to a script run by cron or by a systemd service, because neither ever reads .bashrc. The fix is to set PATH in the script itself, in the cron table, or in the service’s own environment settings, not to source .bashrc from a script.

The SSH exception

One case breaks the “scripts read nothing” rule. A command run over SSH, as in ssh host somecommand, is non-interactive, but bash may read .bashrc anyway. The manual says bash tries to detect when it is run by the remote shell daemon, usually rshd, or the secure shell daemon sshd, and in that case reads ~/.bashrc. The test reproduced the trigger by setting the SSH_CLIENT variable on a plain bash -c true, and it printed read .bashrc. That is why output printed by .bashrc, such as a welcome message, can break tools like scp and rsync, which expect a clean channel: the startup file runs on every remote command.

The system-wide files

/etc/profile and /etc/profile.d

Before any personal file, a login shell reads /etc/profile, as the manual states. On the Ubuntu test system, that file did two more things worth knowing. When the shell is bash and interactive, it sources /etc/bash.bashrc, the system-wide counterpart of .bashrc. And it runs every *.sh file in /etc/profile.d/, in name order. That directory is where packages drop their own environment settings; on the test machine it held scripts for Java, Maven, Gradle and others. A system-wide PATH addition belongs in a new file there, not in an edit to /etc/profile that the next package update may overwrite.

The guard at the top of .bashrc

Ubuntu’s default .bashrc begins with a guard:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

$- holds the shell’s option letters, and i is among them only in an interactive shell; in the test, a plain bash -c showed hBc and bash -i -c showed hiBHc. The guard is what keeps the SSH exception above from doing harm on a stock system: when sshd triggers a read of .bashrc for a remote command, the file returns immediately, before any alias, prompt or message. Anything added above that guard runs for every remote command; anything below it does not. shopt -q login_shell answers the other question, whether the shell is a login shell; it was false for bash -c and true for bash -l -c.

Nighttime desk setup with laptop and lamp

Where PATH and aliases belong

The usual split

  • Environment variables, including PATH, that should apply to everything a user runs: .profile (or .bash_profile), read once at login and inherited by every program started from that session.
  • Aliases, functions, prompt settings and shell options: .bashrc, because they are not inherited by child processes and must be set in every interactive shell.
  • Anything a script or service needs: set in the script or the service definition, not in either file.

The duplicate PATH

Putting export PATH="$HOME/bin:$PATH" in .bashrc instead of the login file leads to the duplicate-entry symptom. Every nested shell sources .bashrc again and prepends the directory again. Sourcing a file with that line twice in one shell turned /usr/bin into /h/bin:/h/bin:/usr/bin. Harmless at first, confusing after a few nested shells, and a sign that the line is in the wrong file. A guard prevents it:

case ":$PATH:" in *":$HOME/bin:"*) ;; *) PATH="$HOME/bin:$PATH" ;; esac

sh is not bash

The same home directory, started as sh -l on Ubuntu, where sh is dash, printed read .profile and nothing else. Dash knows nothing of .bash_profile or .bashrc. The manual makes a related point from bash’s side: when invoked as sh, bash itself skips .bashrc in the SSH case. Anything in .profile should therefore stay plain POSIX shell syntax, because shells other than bash read it.

A short checklist

When a setting does not apply

  • Check which kind of shell is running: shopt -q login_shell && echo login, and echo $- contains i when interactive.
  • If a .bash_profile exists, make sure it sources .bashrc or that nothing important lives only in .profile.
  • For cron and services, assume no startup file is read, and set what is needed explicitly.
  • Keep .bashrc silent; output from it breaks scp and rsync over SSH.

Tracing a login

When none of that explains it, bash can show its own startup. bash -lx -c true starts a login shell with command tracing on, and prints every line it runs from every startup file, each prefixed with +. On the test system, the trace walked through /etc/profile, sourced each script in /etc/profile.d/ one by one, and finally ran export FOO=1 from the test .profile, 179 traced lines in all for a login that did nothing but start and exit. Most of those lines came from system files, not personal ones, which is a useful reminder that a login shell’s environment is assembled from many places before the home directory gets a turn. Searching that output for the variable or alias in question shows which file set it, which file changed it later, and whether the file meant to set it was read at all.

Sources

  • Bash Startup Files, GNU Bash Reference Manual: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *