Archive

Bash Startup

bash has a rather complicated way of working out which init files to read when it starts up. This is documented in the man page, but since that is 900 lines long, this page summarises how it works, so that you can find which file(s) to modify for the effect you want.

Login shell vs normal shell

First, you need to know whether your shell is a login shell or not. As the name suggests, a login shell is the one you get when you log in on the console. You can also obtain a login shell by supplying the –login option to bash when you run it (for example, in the menu entry to run an xterm in your window manager menu).

Unless otherwise specified, you will get a non-login shell – this is usually the sort of shell you get when starting an xterm, for example, or running a shell script.

Startup for Login shell

First of all, bash looks in the file /etc/profile. This is a system-wide file that sets up critical things such as the $PATH environment variable and the system-wide prompt.

Note that on Debian systems (and most likely other Linux distros) PAM might be invoked. This is only relevant from the login shell spawned via login(1). PAM will then read /etc/pam.d/login which might then do various thing — one of which is to read /etc/environment. This is primarily the only shell-agnostic means of defining env vars, and so forth.

When this file is read, the next file bash goes looking for is ~/.bash_profile. This file is a personalised version of /etc/profile and allows you to re-define your prompt, set any shell aliases, etc.

Next, bash looks for ~/.bash_login, and if it cannot find that will then look for ~/.profile . These files are looked for in that order and bash will read the first one it finds and then stop looking. Many files usually allow for the option of ~/.bashrc to be read (see next section). For debian users, this is commented out by default in ~/.bash_profile.

Finally, when a login shell exits, the file ~/.bash_logout is read.

Startup for normal shells

A normal shell (also known as an interactive shell) is one that is invoked not as a login shell. Typically this will be from a shell script, or some terminal emulators (konsole is indicative of this) also launch bash in this way.

If that is the case then none of the files mentioned in the previous section are read, instead the file /etc/bash.bashrc is read. This file usually defines another prompt if need be. After this file is read, bash will then try to read ~/.bashrc .

Typically, if the option in ~/.bash_profile exists for this file to be read as well, then this usually means that for any subsequent bash invocations that take place during the session will read the same file, regardless of it being a login shell or not.

Summary

  • Login shell

    • /etc/profile
    • Then the first of the following that it finds:

      • ~/.bash_profile
      • ~/.bash_login
      • ~/.profile
  • Non-login shell

    • /etc/bash.bashrc
    • ~/.bashrc

Interesting things to note

  • Login shells can be prevented from reading their startup files via the –noprofile flag.
  • Interactive shells can be prevented from reading their startup files via the –norc flag.
  • Command aliases should go in ~/.bashrc — so that they are available to interactive shells.

Page written by: ThomasAdam

Leave a Reply