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?

  • 5
    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
  • There is .profile as well... See this question on Stack Overflow. – Yaakov Baruch Feb 3 '17 at 10:06
  • 2
    If you brew install bash and use iTerm2, you could set profile -> command to /usr/local/bin/bash which will load .bashrc by default after .bash_profile. This also gives you Bash 4 goodies... – Ray Foss Jan 25 '18 at 21:03
  • I heard .bashrc hasn't been used for years, and by default won't work, and .bash_profile is the thing in macosx. And I tried .bashrc and t didn't work for me but .bash_profile did – barlop Mar 8 at 21:00

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

  • 56
    On OS X, Terminal by default runs a login shell every time - I have always been so confused by not realizing this. Great info! – vaughan Sep 6 '17 at 12:18
  • 1
    @Alex, Why would OS X terminal run a login shell every time? – Pacerier Nov 2 '17 at 13:10
  • 14
    'cos some Apple dev didn't realise the difference originally and now it's enshrined in some mumbo jumbo. – Snowcrash Nov 8 '17 at 9:49
  • 2
    I'm on OS X and I use zshell instead of bash, and iTerm instead of Terminal. Despite the fact that I'm using a different terminal and a different shell than the answer discusses, OS X still seems to be considering everything a login shell, because .zprofile gets ran every time. – Adam Zerner Jan 16 '18 at 17:33
  • 1
    For those looking for a thorough explanation of the combinations of login/non-login and interactive/non-interactive shells and when they run these config files, see unix.stackexchange.com/a/46856/38715 – kevinmicke May 28 '18 at 20:20

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
  • Or you could just do cd ~ ; ln -s .bashrc .bash_profile. – lhf May 13 '12 at 23:52
  • 4
    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. – dan Sep 23 '13 at 14:20
  • 2
    @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 '14 at 0:28
  • 13
    @mipadi There is still value to separate them. For example, .bash_profile can never be sourced again in child process. Every level of nested Bash sources .bashrc, so if you put something like export A=a:$A in .bashrc, your $A will get longer in nested Bash. I typically leave environment variable in profile, and aliases in RC. – Franklin Yu Nov 1 '17 at 23:29
  • @FranklinYu May not be that big of a deal for many people, but I totally agree! It is a great technical point to remind people and deserves way more upvotes. – Subfuzion May 8 '18 at 7:35

For macOS, the code to put into .bash_profile to consolidate everything into .bashrc is the following:

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

This is more specific for Mac terminal user.

You must log in to answer this question.

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