I would like to know what is the difference between the files .bashrc
and those .bash_profile
that usually go in the home of users in Linux or Mac OS operating systems.
If they are the same, can you only have one of those files?
In my case, I only have the .bash_profile file in my home and I need to execute the following command:
echo PATH=\"\$NPM_PACKAGES/bin\:\$PATH\" >> ${HOME}/.bashrc
Would it be the same if I change it to the following?
echo PATH=\"\$NPM_PACKAGES/bin\:\$PATH\" >> ${HOME}/.bash_profile
.bash_profile
it runs only once, when you log in..bashrc
is executed every time you start the interpreterbash
.When executing the first command, if the file
.bashrc
does not exist, it will be created. If it already exists, the line is added to the end.Due to the purpose of the line you want to add, it is best to add it to the
.bashrc
.Traditionally, when you login to a Unix system, the system will open a program for you. That program is the shell, that is, a program designed to start other programs. It's a command line shell: you start another program by typing its name. The default shell, the Bourne shell, reads commands from
~/.profile
when it is invoked by the login shell.Bash is a Bourne-like shell. Read commands from
~/.bash_profile
when invoked as the login shell. And if that file doesn't exist¹ try to read~/.profile
.You can invoke a shell directly at any time. For example, running a terminal emulator within a GUI environment. If the shell is not a login shell, then don't read
~/.profile
. When you start bash as an interactive shell (that is, not to run a script), it reads~/.bashrc
(except when it was invoked as a login shell, in which case it only reads~/.bash_profile
or~/.profile
).So:
~/.profile
it is the file where to save the things that apply to the whole session, like the programs that you want to start when you log in (but not graphical programs, since they go in a different file) and environment variable definitions.~/.bashrc
is the file to store things that apply only to bash itself, like aliases and function definitions, as well as prompt settings. (You could also put key bindings here, but in bash they are usually put in~/.inputrc
).~/.bash_profile
can be used instead of~/.profile
, but it is only read by bash and not by the other shells. (This is important to note if you want your initialization files to work on different machines and bash is not the login shell for all of them.) This is the logical place to include~/.bashrc
if the shell is interactive. For this reason, I recommend putting the following in~/.bash_profile
:On modern UNIX systems there is an added complication related to
~/.profile
. If you log in in a graphical environment (that is, if the program where you type your password is running in graphical mode) you don't automatically receive a login shell that reads~/.profile
. Depending on the graphical login program, window manager or desktop environment you run afterward and how your distribution configures these programs, your~/.profile
may read, but may not. If it doesn't read, there's usually another place to define environment variables and programs to launch when you log in, but unfortunately that place doesn't have a standard location.Note that you may read here and there recommendations to put the definition of environment variables in
~/.bashrc
or launch login shells on terminals. Both are bad ideas. The most common problem with these ideas is that your environment variables will only be set in programs launched from the terminal, but not in those that are started directly with an icon, through the menu or with some keyboard shortcut.¹ If
.bash_profile
it doesn't exist, bash also tries.bash_login
before trying.profile
. Feel free to forget that it exists.~/.bashrc is the place to put things that only apply to bash itself, like aliases and function definitions, shell options, and system settings. (You can also put key links there, but for bash they normally go in ~/.inputrc.)
~/.bash_profile can be used instead of ~/.profile, but it is read by bash, not by any other shell. (This is more of a concern if you want the initialization files to work on multiple machines and your login shell isn't bash on all of them.) This is a logical place to include ~/.bashrc if it's your interactive shell. I recommend the following content in ~/.bash_profile: