.bashrc or .bash_profile — and why your commands vanished after you created .bash_profile

Here's what happens. You're on an Ubuntu server. Some guide told you: "add a line to ~/.bash_profile". You create the file, add the line, log back in over SSH.
And suddenly the pipx tools that worked yesterday say command not found.
You didn't delete anything. You only added a file. But once bash saw it, it stopped reading another file — ~/.profile. That's where the line adding ~/.local/bin to your PATH lived. Let's sort out which file gets read when.
In short: the difference
Both files are dotfiles in your home folder, and both are scripts bash runs on startup. The difference is which startup.
.bash_profileis read by a login shell — the shell that opens when you log in. SSH login, a server console login,bash -l..bashrcis read by an interactive non-login shell — when you're already logged in and open a new terminal window on a Linux desktop, or just typebash.
Picture a house with two doors. You come through the front door once in the morning — and hang up your coat and keys there. You run through the side door back and forth all day. Each door has its own note, and you read the one at the door you came through.
Point-by-point comparison
Let's go through what actually matters to a beginner.
- When it's read.
.bash_profile— at login..bashrc— on every new interactive window. - What goes in. In the login file — what's needed once and by every program:
PATH,exportvariables. In.bashrc— what you need at the keyboard: aliases, functions, how your prompt looks. - What else can stand in for
.bash_profile. At login, bash looks for the first existing file out of three:~/.bash_profile,~/.bash_login,~/.profile. Found the first one — it ignores the rest. That's where the commands from the intro went. - How they connect. A login shell doesn't read
.bashrcby itself. Ubuntu solves this: the stock~/.profilecalls.bashrcat the end. We tested it on a clean folder:bash -lread only.profile,bash -ionly.bashrc, and a script read neither. - Scripts and cron. They read neither. That's why an alias from
.bashrcdoesn't work in a script, and a cron job often doesn't see your PATH. - A trap inside
.bashrcitself. The stock Ubuntu file starts with a check: "if the shell isn't interactive, return". Anything you add below it simply won't run for non-interactive launches.
What about Mac and zsh
Since 2019 the Mac ships with zsh by default, not bash. The file names differ, the logic is the same:
~/.zprofile— the counterpart of.bash_profile, for login shells;~/.zshrc— the counterpart of.bashrc, for interactive windows;~/.zshenv— read always, even by zsh scripts. Don't put anything heavy there.
And the Mac's key quirk: the Terminal app opens every new window as a login shell. So both files get read there, and the confusion is less visible. On a Linux desktop a new window is usually non-login — and a line from .bash_profile won't kick in until you log in again.
Which one to use: the verdict
No fence-sitting:
-
PATH and environment variables go in the login file. On Ubuntu that's
~/.profile, on a Mac~/.zprofile. Don't create a.bash_profileon Ubuntu without a reason. -
Aliases, functions, the prompt go in
~/.bashrcor~/.zshrc. -
If you already have a
.bash_profile— make its first line load the old files:[ -f ~/.profile ] && . ~/.profileUbuntu's
.profilewill pull in.bashrcby itself. If you have no.profile, load~/.bashrcdirectly. -
Changed a file — apply it without logging out:
source ~/.bashrc. Whysourceand not running it — see source vs running a script. -
Not sure which file gets read — ask the files themselves. Temporarily make the first line of each one
echo "reading .bashrc",echo "reading .profile"and so on. Open a new window, log in over SSH — and you'll see right away who fired and who stayed silent. Then remove those lines: extra output at login breaksscpandrsync.
Why can't my AI agent see my aliases?
Claude Code, Codex and CI run commands in a non-interactive shell. It doesn't read .bashrc, and sometimes not the login files either. So give the agent full commands and absolute paths, not your shortcuts.
How do I tell whether my shell is a login shell?
In bash run shopt -q login_shell && echo login || echo not-login. Another hint: in echo $0 a login shell usually has a leading dash, like -bash.
Do I even need .bash_profile?
Most of the time, no. On Ubuntu .profile and .bashrc are enough, on a Mac .zprofile and .zshrc. Create it only if you know it will override .profile.
The terminal without the magic — in KODiQ we break things like this down in short lessons with clear examples.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





