The difference between .bashrc and .bash_profile

When you log into a Unix system, the system starts the shell that is set as default. The bash shell reads the ~/.bash_profile at login and if this file is missing or corrupt, the shell tries reading ~/.profile.

Only the login shells read the ~/.profile file. The/bin/false shell, for example, doesn’t.

When you start bash (type bash or /bin/bash in the terminal), the ~/.bashrc file will be read, except when the bash is invoked as the default shell and the ~/.bash_profile or ~/.profile files are read.

So, in the ~/.bashrc file you can put your aliases, functions, or your custom prompt configuration. It is better to add this line in your ~/.bash_profile (if you don’t already have it) , to invoke the ~/.bashrc and the ~/.profile filesĀ  when the ~/.bash_profile is read:

if [ -r ~/.profile ]; then . ~/.profile; fi
case "$-" in *i*) if [ -r ~/.bashrc ]; then . ~/.bashrc; fi;; esac

You can also put the scripts you want to start at login, in the ~/.bash_profile file.

There is also a /etc/bash.bashrc file. In the /etc/bash.bashrc place the commands you want to be available for all the users in the system (for example: a PATH to a script that all the users in the system need to use.)

When you login from the GUI, the ~/.profile isn’t always read by the shell. This depends also on the desktop environment used.

 

Tagged with: , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!