Startup files and login vs interactive shells

Beginner · 10 min

Learn which startup file Bash reads and when: login or not, interactive or not. Each claim was tested with real files, plus the Zsh equivalents.

Before thisThe PATH and how commands are found

Zach Gollwitzer · 10 min

Startup files and login vs interactive shells

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Zach GollwitzerReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Bash decides which startup files to read from two yes/no questions: is this a login shell, and is it interactive?
  • A login shell reads the first one found of ~/.bash_profile, ~/.bash_login and ~/.profile. An interactive non-login shell reads ~/.bashrc. A plain script reads neither.
  • Because a login shell skips ~/.bashrc, the usual fix is a ~/.bash_profile that sources ~/.bashrc.
  • Put environment variables such as PATH in the login file and interactive things like aliases and the prompt in ~/.bashrc.
  • Zsh uses a different set of files: .zshenv always, .zprofile for login, .zshrc for interactive, .zlogin after.

Why there is more than one startup file

When Bash starts, it can run some commands before you see the prompt: set PATH, define aliases, set the prompt. Those commands live in files in your home directory. The trouble is that Bash is started in different situations, and what you want differs. Setting PATH once per login makes sense. Redefining aliases every time you open a new window makes sense too. A script launched by cron wants neither.

Bash solves this with two yes/no questions asked at startup:

  1. Is it a login shell? That is the first shell of a session, such as the one started after you authenticate, or one started with bash -l.
  2. Is it interactive? That means it’s reading commands from a terminal you’re typing at, rather than from a script file or from bash -c.

What I tested

I made a throwaway home directory with four files, each printing its own name (.bash_profile, .bash_login, .profile, .bashrc), and started Bash in an empty environment with HOME pointing there and standard input from /dev/null. -l makes a login shell, -i an interactive one, and -c runs a command string. The body of each command printed whether it was a login shell and whether $- contained i:

$ bash -c ...
  script body: login=no interactive=

$ bash -ic ...
  [read .bashrc]
  body: login=no interactive=i

$ bash -lc ...
  [read .bash_profile]
  body: login=yes interactive=[]

$ bash -lic ...
  [read .bash_profile]
  body: login=yes interactive=[i]

Plain bash -c read nothing. An interactive non-login shell read .bashrc. A login shell read .bash_profile, and not .bashrc, even when it was also interactive. (Starting bash -i with no terminal also prints “no job control” warnings. I filtered those lines out.)

First one found wins

A login shell reads only the first of three files that exists:

$ bash -lc true          # with .bash_profile present
  [read .bash_profile]
$ bash -lc true          # after removing .bash_profile
  [read .bash_login]
$ bash -lc true          # after removing .bash_login
  [read .profile]
$ bash -lc true          # after removing .profile
$

That order, .bash_profile, then .bash_login, then .profile, is why having both .bash_profile and .profile surprises people: once .bash_profile exists, .profile is skipped by Bash. The .profile file is the portable one that other shells such as sh read, so some people keep their settings there and leave .bash_profile absent.

The standard fix

Since login shells skip .bashrc, aliases defined there vanish at login. The usual repair is one line in the login file:

# ~/.bash_profile
[ -f ~/.bashrc ] && . ~/.bashrc

I wrote that as the new .bash_profile and ran a login interactive shell:

$ bash -lic ...
  [read .bash_profile]
  [read .bashrc]
  body

Both were read. A reasonable split: put variables that programs inherit (PATH, EDITOR) in the login file, and aliases, prompt and completion in .bashrc.

Scripts, BASH_ENV and the ssh exception

A script isn’t interactive, so it reads neither file, which is good: your aliases shouldn’t change how a script behaves. There is an escape hatch, BASH_ENV. If it names a file, a non-interactive Bash reads that file first:

$ env BASH_ENV=$H/envfile bash -c 'echo "  body"'
  [read envfile]
  body

I got a surprise doing this. My first attempt, run from a tool whose standard input was a network socket, showed plain bash -c reading .bashrc. That matches the manual: Bash also reads .bashrc when it notices its input is a network connection, which is how the remote shell daemon starts it. After I redirected input from /dev/null the behaviour matched the table above. The practical point is that commands run over ssh host command may load .bashrc, so a .bashrc that prints text is a known way to break scp and rsync. I did not test that part.

The flags --norc and --noprofile skip the files, which is how I got a clean slate: bash --norc -ic ... and bash --noprofile -lc ... printed only the body. A login shell also reads ~/.bash_logout on exit, and I confirmed that with a [read .bash_logout] line after exit.

Zsh does it differently

I ran the same experiment with Zsh and four marker files:

$ zsh -c ...
  [read .zshenv]
$ zsh -lc ...
  [read .zshenv]  [read .zprofile]  [read .zlogin]
$ zsh -ic ...
  [read .zshenv]  [read .zshrc]
$ zsh -lic ...
  [read .zshenv]  [read .zprofile]  [read .zshrc]  [read .zlogin]

.zshenv is read by every Zsh, even scripts. A Zsh login shell, unlike Bash, also reads .zshrc when interactive, so Zsh users rarely hit the missing-alias problem. Keep that in mind if you move between the two.

About the video

Zach Gollwitzer’s “The difference between .bashrc, .bash_profile, .profile, and .bash_login (login vs non login shells)” was published 14 March 2019 and runs 10 minutes 28 seconds. Its description gives chapters: “What is a Login shell?” at 0:52, a login demonstration at 4:44, “Non-Login Shell” at 6:35, and “Setting your system defaults” at 9:27. It is in English by title and description. I couldn’t read a transcript. It is from a Bash course and is presumably aimed at Linux, so I can’t say how it treats macOS Terminal. It is seven years old; the startup rules haven’t changed, but it won’t mention Zsh. The short You Suck at Programming video on login shells is the runner-up.

What I did not run

I did not open a real Terminal.app or SSH session. People often say Terminal.app starts a login shell on macOS, and I have not verified that here. Linux display managers, su -, and sudo -i are also untested.

Next

You now know where the shell’s settings live and when they load. The next group works with the files themselves, starting with listing and inspecting files.

Check yourself

  1. 01You added an alias to ~/.bashrc, but it is missing after you log in over a console. Why?

    A login shell reads ~/.bash_profile (or the next file in line) and not ~/.bashrc. Source ~/.bashrc from your login file.

  2. 02If ~/.bash_profile and ~/.profile both exist, does a Bash login shell read both?

    No. It reads the first one it finds in the order .bash_profile, .bash_login, .profile and stops. ~/.profile is ignored when ~/.bash_profile exists.

  3. 03Does a script you run with bash myscript.sh read ~/.bashrc?

    Normally no. It is neither a login nor an interactive shell. The exception is when Bash is started by sshd, where it does read it.

  4. 04How can you check from inside a shell whether it is a login shell?

    Run shopt -q login_shell && echo yes || echo no. For interactive, test whether $- contains the letter i.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark