Part III · Shell and commands
27.2 Shell startup files
alsobashrc.bashrcbash_profile.bash_profile.profilezshrc.zshrcstartup filesdotfileslogin shell
TL;DR
- A login shell (console, SSH) reads the profile files. bash reads only the
first that exists of
~/.bash_profile,~/.bash_loginand~/.profile. - An interactive shell that isn't a login shell, such as a new terminal tab
on most desktops, reads
~/.bashrc. - zsh reads
~/.zprofilefor login shells and~/.zshrcfor every interactive shell.
echo $0 # "-bash" with a dash: a login shell
source ~/.bashrc # reload after editing
On your machine
$ atfq 27.2 Shell /bin/zsh Files ~/.bash_profile, ~/.profile, ~/.bashrc, ~/.zshrc, /etc/profile, /etc/profile.d/ (2 files) Bash bash login shells read ~/.bash_profile and ignore ~/.profile
An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.
Pitfalls
- When
~/.bash_profileexists, bash login shells ignore~/.profilecompletely. Have~/.bash_profilesource~/.bashrcto get both. - Scripts are not interactive: they read none of these files. Set what a script needs inside the script.
- bash also reads
~/.bashrcfor commands run over SSH: output from it (anecho) breaksscpandrsync. Print only in interactive shells. - A change to
~/.profileonly applies at the next login, not in a new terminal tab.
Deep dive 2 questions
Why two kinds of files?
The profile files run once per session, to set environment variables that
every program started later inherits. The rc files run in every
interactive shell, for what isn't inherited: aliases, functions, the
prompt, completion. So export goes in the profile, alias in the rc.
And the files in /etc?
/etc/profile and the scripts in /etc/profile.d/ run for every user's
login shell. bash's system-wide rc file is /etc/bash.bashrc on Debian and
Ubuntu, /etc/bashrc on Fedora. To change something for everyone, add a
small file to /etc/profile.d/ rather than editing /etc/profile.