devutils/ATFQ/book ATFQ git

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_login and ~/.profile.
  • An interactive shell that isn't a login shell, such as a new terminal tab on most desktops, reads ~/.bashrc.
  • zsh reads ~/.zprofile for login shells and ~/.zshrc for every interactive shell.
echo $0               # "-bash" with a dash: a login shell
source ~/.bashrc      # reload after editing

On your machine

alice@laptopexample
$ 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

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.

Commands
source
Paths
~/.bashrc ~/.profile /etc/profile /etc/profile.d/
See also
27.1 Where environment variables come from
23.3 PATH and command resolution