Linux › Section M · User environments › Topic M.2

Startup file order, defaults, and pitfalls

Bash can read four different personal startup files, and which ones it reads depends on how the shell was started. Getting the order right is the difference between a setting that always works and one that mysteriously vanishes over SSH.

Why order matters

The previous topic, "Startup file guidelines and elements", decided what goes in a startup file: PATH, the prompt, the umask and a few defaults. The harder question is which file to put them in. Bash recognises .bash_profile, .bash_login, .profile and .bashrc in your home directory, and it does not read all of them every time. It sorts shells along two lines: interactive or not (are you typing commands at it?) and login or non-login (is it the first shell of a session?). The answer decides which files run.

Login shells

A login shell is the first shell of a session: the one you get after logging in on a text console or connecting with ssh to an interactive session. The GNU Bash manual gives its order exactly. It first runs /etc/profile, if it exists. It then looks for ~/.bash_profile, ~/.bash_login and ~/.profile, in that order, and runs only the first one it finds and can read. Starting bash with -l or --login makes even a non-interactive shell behave as a login shell, and --noprofile skips these files. When a login shell exits, bash runs ~/.bash_logout if it exists.

Non-login and non-interactive shells

An interactive shell that is not a login shell, such as the one in a new terminal window or one you start by typing bash, reads only ~/.bashrc (--norc skips it; --rcfile names another file). Debian and Ubuntu build bash to read /etc/bash.bashrc first; on Fedora and RHEL the default ~/.bashrc runs /etc/bashrc itself. A non-interactive shell, such as one running a script, reads no startup file except the one named in the BASH_ENV variable. One special case: when bash detects it was started by sshd to run a single remote command, it reads ~/.bashrc. If bash is invoked as sh, it imitates the Bourne shell: /etc/profile and ~/.profile at login, and the file named in ENV when interactive.

The consequence: put everything in .bashrc

A login shell never reads .bashrc on its own, and a terminal-window shell never reads .bash_profile. Settings in only one of them are missing half the time. The robust arrangement is to keep your real settings in ~/.bashrc and make ~/.bash_profile a one-line bridge: . "$HOME/.bashrc" (the dot command runs a file in the current shell). Distributions already do a version of this: Fedora's default ~/.bash_profile sources ~/.bashrc, and Debian's default ~/.profile does so when the shell is bash. If you create a .bash_profile on Debian, remember it now hides .profile, because only the first file found runs.

Worked example: watching the order

To see the rules in action, make a test home whose files only announce themselves. 1. In /tmp/h2, create .bash_profile, .bash_login, .profile and .bashrc, each containing one line such as echo reading .bashrc. 2. Run HOME=/tmp/h2 bash -l -c 'echo cmd'. In bash 5.2 the output was reading .bash_profile then cmd: only the first file found ran. 3. Delete .bash_profile and repeat: now reading .bash_login appeared. 4. Start an interactive non-login shell with HOME=/tmp/h2 bash -i: only reading .bashrc appeared. 5. Run HOME=/tmp/h2 bash -c 'echo cmd': just cmd, with no startup file at all. 6. Add BASH_ENV=/tmp/h2/.bashrc to that command and reading .bashrc returns.

Testing what kind of shell you are in

Two checks let a startup file adapt. The special parameter $- lists the shell's current option letters, and it contains i only in an interactive shell. A case statement tests it: case $- in *i*) echo interactive ;; *) echo non-interactive ;; esac. Run as a script, this printed non-interactive; run with bash -i, it printed interactive. For the login question, shopt -q login_shell succeeds only in a login shell; in tests, bash -i reported non-login and bash -i -l reported login. A login shell started by login or sshd also shows a leading hyphen in $0, as in -bash, but bash -l does not, so shopt is the more reliable test.

Other shells: tcsh and zsh

The C shell family is simpler. tcsh reads ~/.tcshrc for every shell, falling back to ~/.cshrc if .tcshrc is missing; login shells also read ~/.login afterwards. Its syntax differs from bash: setenv PATH ... instead of export, and set prompt="..." with escapes such as %m for the host name. zsh, popular as an interactive alternative, reads ~/.zshenv for every shell, ~/.zprofile and ~/.zlogin for login shells and ~/.zshrc for interactive ones. Whatever shell you support, the same rule applies: know which file runs in which case before you add a line.

Default user settings

Bash is the right default shell for new accounts: users type commands in the same language they write scripts in, it is the distribution default, and its readline editing matches many other tools. Users who prefer zsh or fish can switch with chsh -s and any shell listed in /etc/shells. For the editor, vi is present on nearly every Unix-like system, while many distributions configure nano as the friendlier default; keep editor startup files such as .vimrc tiny. Set PAGER to less. Test new defaults on a fresh test account, never by copying your own files: log in on the console, over ssh and graphically, then copy the files to a second test user and repeat.

Worked example: an SSH copy that breaks

A user adds echo "Welcome back!" to ~/.bashrc. Soon scp and rsync to that machine fail with strange protocol errors. Diagnose it step by step. 1. scp and rsync run a remote command over ssh, so the remote side starts a non-interactive bash. 2. Because that bash was started by sshd, it reads ~/.bashrc. 3. The echo writes text into the very channel the file-copy protocol uses, so the client sees garbage where it expects protocol data. 4. Fix: print nothing from startup files, or wrap the line in the interactive test, case $- in *i*) echo "Welcome back!" ;; esac, so it runs only at a real prompt.

Common mistakes to avoid

Printing text from a startup file, which breaks scp, sftp and rsync as above. Running graphical programs or setting DISPLAY in a shell startup file: not every shell has a display, and the graphical session sets DISPLAY itself. Setting TERM, which the terminal program already reports correctly. Setting LD_LIBRARY_PATH, which makes every program search extra library directories and can load the wrong library version; the topic "The C compiler and linking" shows better ways. Putting settings only in .bash_profile and wondering why new terminal windows ignore them. Skimping on comments in files other users inherit.

Key takeaways

A bash login shell runs /etc/profile and then the first of ~/.bash_profile, ~/.bash_login and ~/.profile; an interactive non-login shell runs ~/.bashrc; scripts read only BASH_ENV, if set. Keep settings in ~/.bashrc and source it from ~/.bash_profile. Use $- to detect an interactive shell and shopt login_shell to detect a login shell. tcsh uses .tcshrc or .cshrc; zsh uses .zshenv, .zprofile and .zshrc. Make bash the default, test defaults on fresh accounts, and never print output, set DISPLAY or TERM, or set LD_LIBRARY_PATH in startup files.

Flashcards

Flashcard

Login shell

The first shell of a session, from a console login or interactive ssh

bash -l or --login forces one.

Test your understanding

Question 1 of 5

A home directory contains .bash_profile, .profile and .bashrc. Which personal file does an interactive login shell read by itself?

Question 2 of 5

Settings placed only in ~/.bash_profile appear after an ssh login but not in new terminal windows. Why?

Question 3 of 5

Which test in a startup file is true only when the shell is interactive?

Question 4 of 5

After a user adds an echo line to ~/.bashrc, rsync to that machine fails. What is the best explanation?

Question 5 of 5

Which line belongs in a shell startup file according to this topic?

← Previous topicNext topic →