The main difference with shell config files is that some are only read by "login" shells (eg. when you login from another host, or login at the text console of a local unix machine). these are the ones called, say, .login or .profile or .zlogin (depending on which shell you're using).

Then you have config files that are read by "interactive" shells (as in, ones connected to a terminal (or pseudo-terminal in the case of, say, a terminal emulator running under a windowing system). these are the ones with names like .bashrc, .tcshrc, .zshrc, etc.

bash complicates this in that .bashrc is only read by a shell that's both interactive and non-login, so you'll find most people end up telling their .bash_profile to also read .bashrc with something like

[[ -r ~/.bashrc ]] && . ~/.bashrc

Other shells behave differently - eg with zsh, .zshrc is always read for an interactive shell, whether it's a login one or not.

The manual page for bash explains the circumstances under which each file is read. Yes, behaviour is generally consistent between machines.

.profile is simply the login script filename originally used by /bin/sh. bash, being generally backwards-compatible with /bin/sh, will read .profile if one exists.

Answer from Cos on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 415403 › whats-the-difference-between-bashrc-bash-profile-and-environment
shell - What's the difference between .bashrc, .bash_profile, and .environment? - Stack Overflow

The main difference with shell config files is that some are only read by "login" shells (eg. when you login from another host, or login at the text console of a local unix machine). these are the ones called, say, .login or .profile or .zlogin (depending on which shell you're using).

Then you have config files that are read by "interactive" shells (as in, ones connected to a terminal (or pseudo-terminal in the case of, say, a terminal emulator running under a windowing system). these are the ones with names like .bashrc, .tcshrc, .zshrc, etc.

bash complicates this in that .bashrc is only read by a shell that's both interactive and non-login, so you'll find most people end up telling their .bash_profile to also read .bashrc with something like

[[ -r ~/.bashrc ]] && . ~/.bashrc

Other shells behave differently - eg with zsh, .zshrc is always read for an interactive shell, whether it's a login one or not.

The manual page for bash explains the circumstances under which each file is read. Yes, behaviour is generally consistent between machines.

.profile is simply the login script filename originally used by /bin/sh. bash, being generally backwards-compatible with /bin/sh, will read .profile if one exists.

Answer from Cos on stackoverflow.com
🌐
Linuxize
linuxize.com › post › bashrc-vs-bash-profile
.bashrc vs .bash_profile | Linuxize
May 10, 2020 - When invoked, Bash reads and executes commands from from a set of startup files. .bash_profile is read and executed when Bash is invoked as an interactive login shell, while .bashrc is executed for interactive non-login shell.
Discussions

terminal - What is the difference between .bash_profile and .bashrc? - Ask Different

🌐 apple.stackexchange.com
May 10, 2012
To make an alias for the Terminal in OS X, you can either put the aliases in .bash_profile or .bashrc. What is the difference between the two and why would I choose to put aliases in one and not the More on apple.stackexchange.com

bash_profile or bashrc??

🌐 r/linux4noobs
4
March 4, 2022
.bash_profile is read and executed when Bash is invoked as an interactive login shell, while .bashrc is executed for an interactive non-login shell. Basically, use .bash_profile for things that should run just once after login (there is not much sense setting PATH each time you open the new shell, for example, as it will be inerited anyway). .bashrc for things you want to start always. More on reddit.com

login - Why have both .bash_profile and .bashrc? - Unix & Linux Stack Exchange

🌐 unix.stackexchange.com
February 20, 2017
What is the point of having both .bash_profile and .bashrc, with the former typically sourcing the latter, which in turn sources /etc/bashrc upon login? What would be the downside of putting all th... More on unix.stackexchange.com

bash_profile or bashrc?? : linux4noobs

🌐 r/linux4noobs
What is the difference between bash_profile and bashrc environment variables? More on old.reddit.com
🌐
Super User
superuser.com › questions › 789448 › choosing-between-bashrc-profile-bash-profile-etc
unix - Choosing between .bashrc, .profile, .bash_profile, etc - Super User

TL;DR:

  • ~/.bash_profile should be super-simple and just load .profile and .bashrc (in that order)

  • ~/.profile has the stuff NOT specifically related to bash, such as environment variables (PATH and friends)

  • ~/.bashrc has anything you'd want at an interactive command line. Command prompt, EDITOR variable, bash aliases for my use

A few other notes:

  • Anything that should be available to graphical applications OR to sh (or bash invoked as sh) MUST be in ~/.profile

  • ~/.bashrc must not output anything

  • Anything that should be available only to login shells should go in ~/.profile

  • Ensure that ~/.bash_login does not exist.

Answer from Dan Rabinowitz on superuser.com
🌐
Stack Exchange
apple.stackexchange.com › questions › 51036 › what-is-the-difference-between-bash-profile-and-bashrc
terminal - What is the difference between .bash_profile and .bashrc? - Ask Different

.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.

But, if you’ve already logged into your machine and open a new terminal window (xterm) then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.

On OS X, Terminal by default runs a login shell every time, so this is a little different to most other systems, but you can configure that in the preferences.

Answer from Alex on apple.stackexchange.com
🌐
Reddit
reddit.com › r/linux4noobs › bash_profile or bashrc??
r/linux4noobs on Reddit: bash_profile or bashrc??
.bash_profile is read and executed when Bash is invoked as an interactive login shell, while .bashrc is executed for an interactive non-login shell. Basically, use .bash_profile for things that should run just once after login (there is not much sense setting PATH each time you open the new shell, for example, as it will be inerited anyway). .bashrc for things you want to start always.
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › bashrc vs. bash_profile: what is the difference?
bashrc vs. bash_profile: What Is the Difference? | phoenixNAP KB
January 23, 2023 - After, the shell searches for the ~/.bashrc configuration file for the specific user. The .bash_profile file is a hidden script file with custom configurations for a user terminal session.
Find elsewhere
🌐
Ask Ubuntu
askubuntu.com › questions › 1411833 › what-goes-in-profile-and-bashrc
What goes in ~/.profile and ~/.bashrc? - Ask Ubuntu

It helps to understand which files get sourced, when, and why.

  • .profile is sourced by a login shell on startup. Typically, the only login shell you start is the one started when you log in, but you can run a login shell at any time with bash -l. (Also, on macOS, there is no initial login shell, so terminal emulators tend to run a login shell for each new window.)

  • .profile is an ideal place to set environment variables that can be inherited by any program started from the login shell.

  • .bashrc, on the other hand, is sourced by non-login interactive shells, such as those started by terminal windows (most configs also source .bashrc in interactive login shells though). This is where you set things specific to your interactive shell that aren't otherwise inherited from the parent process. For example, PS1 is set here because only interactive shells care about its value, and any interactive shell will source .bashrc anyway, so there is no need to define and export PS1 from .profile.

  • And though you didn't ask, it's worth pointing out the difference between .profile and .bash_profile here. .profile is "shared" by all POSIX shells (such as dash), so don't put anything bash-specific here. .bash_profile, though, is only used by bash, so you can use bash extensions in it. If .bash_profile is present, .profile will be ignored, so if for whatever reason you want to use both, you can add . ~/.profile to the top of your .bash_profile.

Answer from chepner on askubuntu.com
🌐
RedSwitches
redswitches.com › home › operating systems › the major differences between bashrc vs bash_profile in linux
Difference Between Bashrc Vs Bash_profile In Linux
January 14, 2024 - The bashrc file is often used to customize the behavior of the shell prompt and to define settings for non-login, non-interactive shells. Conversely, the bash_profile file is commonly used to set environment variables and execute shell commands ...
🌐
Joshstaiger
joshstaiger.org › archives › 2005 › 07 › bash_profile_vs.html
.bash_profile vs .bashrc | Josh Staiger ☙
When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt. But, if you’ve already logged into your machine and open a new terminal window (xterm) inside Gnome ...
🌐
Server Fault
serverfault.com › questions › 261802 › what-are-the-functional-differences-between-profile-bash-profile-and-bashrc
linux - What are the functional differences between .profile .bash_profile and .bashrc - Server Fault

.bash_profile and .bashrc are specific to bash, whereas .profile is read by many shells in the absence of their own shell-specific config files. (.profile was used by the original Bourne shell.) .bash_profile or .profile is read by login shells, along with .bashrc; subshells read only .bashrc. (Between job control and modern windowing systems, .bashrc by itself doesn't get used much. If you use screen or tmux, screens/windows usually run subshells instead of login shells.)

The idea behind this was that one-time setup was done by .profile (or shell-specific version thereof), and per-shell stuff by .bashrc. For example, you generally only want to load environment variables once per session instead of getting them whacked any time you launch a subshell within a session, whereas you always want your aliases (which aren't propagated automatically like environment variables are).

Other notable shell config files:

/etc/bash_profile (fallback /etc/profile) is read before the user's .profile for system-wide configuration, and likewise /etc/bashrc in subshells (no fallback for this one). Many systems including Ubuntu also use an /etc/profile.d directory containing shell scriptlets, which are . (source)-ed from /etc/profile; the fragments here are per-shell, with *.sh applying to all Bourne/POSIX compatible shells and other extensions applying to that particular shell.

Answer from geekosaur on serverfault.com
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › bashrc-vs-bash_profile-what-is-the-difference
bashrc vs. bash_profile: What Is the Difference? - GeeksforGeeks
January 29, 2024 - The ".bashrc" and ".bash_profile" ... While ".bashrc" is focused on customizing the terminal environment for each session, "bash_profile" is dedicated to tasks that should run only once during the login process....
🌐
Vegastack
vegastack.com › tutorials › bashrc-vs-bash-profile
.bashrc vs .bash_profile
October 19, 2023 - You can generate a startup file if one does not exist on your system. No, they serve different purposes. .bashrc is sourced for each new interactive shell, while .bash_profile is sourced only for login shells.
🌐
Baeldung
baeldung.com › home › scripting › difference between .bashrc, .bash-profile, and .profile
Difference Between .bashrc, .bash-profile, and .profile | Baeldung on Linux
March 18, 2024 - On every interactive login, the Bash shell executes .bash_profile. If .bash_profile is not found in the home directory, Bash executes the first readable file found from .bash_login and .profile. Whereas, on every interactive non-login shell startup, Bash executes .bashrc.
🌐
Lei Mao's Log Book
leimao.github.io › blog › bashrc-VS-profile-VS-bash_profile
~/.bashrc VS ~/.profile VS ~/.bash_profile - Lei Mao's Log Book
October 26, 2020 - However, if ~/.bash_profile or ~/.bash_login exists, ~/.profile will not be executed. The default content of the ~/.profile is simple. In most cases, it inherits whatever is in the ~/.bashrc, and adds some user specific executable filepath to the environment variable PATH.
🌐
Slashroot
slashroot.in › difference-between-bashrc-and-bashprofile
Difference Between .bashrc and .bash_profile
May 1, 2018 - It searches for a file named .bashrc (under the home directory of the user), and if found, it executes it. Two different files because they serve different purposes. Things that are specific to your login session should go inside .bash_profile.
🌐
Bhavith C
bhavithc.com › posts › bashrc-vs-bash-profile
.bashrc Vs .bash_profile | Bhavith C
May 17, 2022 - Each Unix like operating system have their own conventions, some OS use .profile ** instead of **.bash_profile · If your shell is bash then you have .bashrc, similarly if you using zsh then rc file will be .zshrc. Even vim and other applications like task uses same method to give user the flexibility to do some initialization stuff.
🌐
TutorialsPoint
tutorialspoint.com › bashrc-vs-bash-profile-what-is-difference
bashrc vs. bash_profile What Is Difference
April 11, 2023 - Bashrc is used to customize your shell environment for each individual terminal window. This means that any changes you make to bashrc will affect only current terminal window. On other hand, bash_profile is used to set environment variables that are needed for entire session.
🌐
Stack Exchange
unix.stackexchange.com › questions › 346336 › why-have-both-bash-profile-and-bashrc
login - Why have both .bash_profile and .bashrc? - Unix & Linux Stack Exchange

Only .bashrc is run on non-login shells, while only .bash_profile is run on login shells.

.bashrc should typically contain things you want to set in every shell you open, like aliases, functions, etc. These are per shell session items that are not inherited from environment.

.bash_profile should contain things that need to be defined at login time only, like PATH and other environment variables, startup programs, etc. You just need things once, not in every shell you open. In most cases, you also need the things from .bashrc in your login shell. That's why .bash_profile sources .bashrc as well, but .bashrc doesn't usually source .bash_profile.

/etc/bashrc and /etc/profile are system wide settings made by the sys admin or the package manager. /etc/profile is sourced automatically in each login shell, before ~/.bash_profile. /etc/bashrc is not sourced, so it needs to be sourced from ~/.bashrc when required.

Now, you can club the two into a single file and link the other file to the first one. But you have to make sure that PATH and other variables are not relative defined (like PATH=$HOME/bin:$PATH) otherwise they will just keep becoming needlessly bigger. Also, you have to be careful of starting programs repeatedly. It's just easier to have these two separate.

Relevant section from the bash man page:

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 a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

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.

Answer from Munir on unix.stackexchange.com
🌐
GitHub
gist.github.com › korya › bd0de47f36dbb5c29a90
.bashrc vs .bash_profile · GitHub
You can invoke a shell directly at any time, for example by launching a terminal emulator inside a GUI environment. If the shell is not a login shell, it doesn't read ~/.profile. When you start bash as an interactive shell (i.e., not to run a script), it reads ~/.bashrc (except when invoked as a login shell, then it only reads ~/.bash_profile or ~/.profile).