2

This question already has an answer here:

I was viewing my .bash_profile file, and I saw this written inside a block of if statement:

if [ -f ~/.bashrc ]; then
       . ~/.bashrc 

Can someone explain what the second line is actually doing?

marked as duplicate by terdon Feb 1 '15 at 21:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

The second line “sources” the .bashrc script; that means it loads and executes its contents in the same shell context. The result is the same as putting the contents of .bashrc in .bash_profile.

This statement is useful because the two files are used in different contexts: .bash_profile is executed when bash starts as a login shell, whereas .bashrc is executed when bash is started as a non-login interactive shell. Including .bashrc in .bash_profile allows it to be the single place for you to add customisation which you want to have in all interactive shells.

If .bashrc wasn’t sourced using ., its execution would spawn a new shell just for the duration, and its effects would be lost immediately.

  • Can . be used for sourcing all kinds of files? – Dhruv Mullick Jan 31 '15 at 19:52
  • 2
    As long as the files are shell scripts, yes! For example it’s handy to build up a library of shell functions. You can see it used in many init scripts (in /etc/init.d/) to load the LSB functions (/lib/lsb/init-functions) or to load external configuration files (such as those in /etc/default). – Stephen Kitt Jan 31 '15 at 19:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.