BASH Shell Notes

The BASH Environment

BASH executes in this order

  1. /etc/environment
  2. /etc/profile
  3. /etc/bash.bashrc
  4. /home/user/.profile
  5. /home/user/.bashrc

Interactive vs Non-interactive Shell

interactive execution order: /etc/profile then the first readable of ~/.bash_profile, ~/.bash_login, and ~/.profile
.profile – is read when an interactive login shell is started.

/etc/profile
~/.bash_profile
~/.bash_login
~/.profile

non-interactive execution order: /etc/bash.bashrc then ~/.bashrc
~/.bashrc – is read when a non-login interactive shell is started

/etc/bash.bashrc
~/.bashrc

Environment variables are conventionally placed in /etc/profile or ~/.profile so that all bourne-compatible shells can use them.

Personal aliases are preferably stored in /etc/bash.bashrc (system-wide) or ~/.bashrc (user).

File Details

/etc/profile

Loads application-specific settings

When invoked interactively with the –login option or when invoked as sh, Bash reads the /etc/profile instructions. These usually set the shell variables PATH, USER, MAIL, HOSTNAME and HISTSIZE.

On some systems, the umask value is configured in /etc/profile; on other systems this file holds pointers to other configuration files such as:

/etc/inputrc, the system-wide Readline initialization file where you can configure the command line bell-style.

the /etc/profile.d directory, which contains files configuring system-wide behavior of specific programs.

/etc/bashrc

On systems offering multiple types of shells, it might be better to put Bash-specific configurations in this file, since /etc/profile is also read by other shells, such as the Bourne shell. Errors generated by shells that don’t understand the Bash syntax are prevented by splitting the configuration files for the different types of shells. In such cases, the user’s ~/.bashrc might point to /etc/bashrc in order to include it in the shell initialization process upon login.

You might also find that /etc/profile on your system only holds shell environment and program startup settings, while /etc/bashrc contains system-wide definitions for shell functions and aliases. The /etc/bashrc file might be referred to in /etc/profile or in individual user shell initialization files.

~/.bash_profile

This is the preferred configuration file for configuring user environments individually

~/.bash_login

This file contains specific settings that are normally only executed when you log in to the system. In the example, we use it to configure the umask value and to show a list of connected users upon login

~/.profile

In the absence of ~/.bash_profile and ~/.bash_login, ~/.profile is read.

~/.bashrc

Today, it is more common to use a non-login shell, for instance when logged in graphically using X terminal windows. Upon opening such a window, the user does not have to provide a user name or password; no authentication is done. Bash searches for ~/.bashrc when this happens, so it is referred to in the files read upon login as well, which means you don’t have to enter the same settings in multiple files.

~/.bash_logout

This file contains specific instructions for the logout procedure.

You may also like...