Difference between /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile and ~/.bashrc

Oftentimes, linux user need to manually edit $PATH environment variable. Linux has several places where $PATH can be modified, such as per-system /etc/profile and per-account ~/.bash_profile, ~/.bash_login, ~/.profile and ~/.bashrc. In this article we describe the difference between the files and answer the concrete question: which of them should be used to modify $PATH variable.

Table Of Contents

Interactive vs Non-interactive bash shells

A shell running a script is always a non-interactive shell.

An interactive shell reads commands from user input on a tty. Among other things, such a shell reads startup files on activation, displays a prompt. The user can interact with the shell.

For more info, refer here.

Config files read by bash

bash

Depending if the bash is called as interacive or non-interactive, different config files are read.

When bash runs as non-interactive, it reads ~/.bashrc and executes the commands in it.

When bash runs as interactive, it tries to execute files in the following sequence: /etc/profile, ~/.bash_profile, ~/.bash_login and then ~/.profile.

Usually ~/.profile includes ~/.bashrc if it exists when bash is default shell. This means, ~/.bashrc is executed either by interactive or non-interactive bash shells:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

Editing $PATH

The place to modify $PATH variable is in ~/.profile to make values of $PATH be seen in interactive and non-interactive shells.

linux 

See also