5

I work with several flavors of linux (CentOS, SuSe and Ubuntu) and every time I need to mess around PATH I came across the problem of not knowing where are things defined by default.

From what I know ~/.bashrc, ~/.bash_profile and /etc/profile are part of the equation. Does anyone know the uses, and differences between these files?

codeforester
  • 39,467
  • 16
  • 112
  • 140
mors
  • 477
  • 4
  • 13
  • 2
    See http://serverfault.com/questions/261802/profile-vs-bash-profile-vs-bashrc – lurker Sep 13 '13 at 16:58
  • 3
    possible duplicate of [What's the difference between .bashrc, .bash\_profile, and .environment?](http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment) – jgb Sep 13 '13 at 17:14

3 Answers3

8

For Bash, they work as follows. Read down the appropriate column. Executes A, then B, then C, etc. The B1, B2, B3 means it executes only the first of those files found.

+----------------+-----------+-----------+------+
|                |Interactive|Interactive|Script|
|                |login      |non-login  |      |
+----------------+-----------+-----------+------+
|/etc/profile    |   A       |           |      |
+----------------+-----------+-----------+------+
|/etc/bash.bashrc|           |    A      |      |
+----------------+-----------+-----------+------+
|~/.bashrc       |           |    B      |      |
+----------------+-----------+-----------+------+
|~/.bash_profile |   B1      |           |      |
+----------------+-----------+-----------+------+
|~/.bash_login   |   B2      |           |      |
+----------------+-----------+-----------+------+
|~/.profile      |   B3      |           |      |
+----------------+-----------+-----------+------+
|BASH_ENV        |           |           |  A   |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|~/.bash_logout  |    C      |           |      |
+----------------+-----------+-----------+------+

In more detail is this excellent flowchart from http://www.solipsys.co.uk/new/BashInitialisationFiles.html : enter image description here

bit_cracker007
  • 2,341
  • 1
  • 26
  • 26
2

/etc/profile is global configuration for login shells (interactive or not), ~/.bash_profile is per-user configuration for login shells, and ~/.bashrc is configuration for interactive non-login shells.

From man bash:

Invocation

[...]

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

[...]

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81
1

.bashrc defines the shell environment for a specific user. It runs every time bash starts, regardless of what mode it runs in, such as an interactive shell, running from a remote call like ssh user@host cat /etc/hosts or even a mere shell script.

.bash_profile is a per-user login profile. It runs once when you login and does things that a user needs when they interact with the system, such as the message of the day or a list of new messages.

/etc/profile is a system-wide login profile script. It also runs once when you login, but runs for all users before their user-specific .bash_profile

For something environmental like a PATH variable, .bashrc or /etc/bash.bashrc is a good place so that you don't have to worry about login or non-login shells.

This page has some details: http://stefaanlippens.net/bashrc_and_others

Brandon
  • 9,822
  • 3
  • 27
  • 37