Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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 other?

share|improve this question
2  
Not to discourage the unix knowledge here, but bash is a pure UNIX beast so you might get better knowledge or have this question answered several times over on a partner site. There are well over 200 bashrc question/answer pairs over on unix –  bmike May 10 '12 at 23:02
add comment

2 Answers

up vote 10 down vote accepted

.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.

share|improve this answer
add comment

X11 will look at your .bashrc while a "regular" Terminal will look at .bash_profile

However, if you add the following to your .bash_profile, you can then move everything into your .bashrc file so as to consolidate everything into one place instead of two:

if [ -f $HOME/.bashrc ]; then
        source $HOME/.bashrc
fi
share|improve this answer
    
Or you could just do cd ~ ; ln -s .bashrc .bash_profile. –  lhf May 13 '12 at 23:52
    
These 2 configuration files have a clearly separate function. In some cases, it is necessary to have things to initialize at the beginning of session and only there (~/.bash_profile). It is also often necessary to have things to define incrementaly at every shell level (~/.bashrc). It isn't the best idea to suggest to suppress this freedom. –  daniel Azuelos Sep 23 '13 at 14:20
    
@danielAzuelos: Lurch left this part out, but the OS X Terminal sources ~/.bash_profile for every new window/tab, so there's not really a way to separate the two as far as Terminal is concerned. –  mipadi Jan 28 at 0:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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