3

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?

2

1 Answer 1

7

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.

2
  • Can . be used for sourcing all kinds of files? Jan 31, 2015 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). Jan 31, 2015 at 19:58

Not the answer you're looking for? Browse other questions tagged .