240

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
239

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

  • 57
    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
  • 16
    '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
43

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
  • 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
21

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

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

This is more specific for Mac terminal user.

0

TLDR; use .bash_profile for your aliases.

The way the different initialisation files work together is a bit more complicated, and there are some important special cases in OSX. Here are the highlights:

  • Bash, on any platform, executes one of several different files depending on how it is invoked. The details are here.
  • OSX's Terminal App does something non-standard: it creates every new tab or window as if it were a login shell, which means that .bash_profile is called. Thus the TLDR advice above.
  • .bashrc is also an option, but that will be called every time you create a subshell (i.e., invoke bash), which can create inefficiency if you update a variable within it (e.g., PATH=/bin/foo:$PATH)
  • Other apps that have embedded terminals can choose to follow Terminal App's convention or not. Notably, Visual Studio Code, by default, does not.
  • Apps invoked via the GUI are not spawned from a shell. Thus, there are several competing mechanisms for setting environment variables for them to see, which have changed over the years.
  • Snippets that call .bashrc from .bash_profile are quite common. I don't recommend that, but it is a preference.
  • Why exactly is using .bash_profile for aliases complicated? The items you list only partially are about aliases at all, so instead of just listing some bullets which might explain why it seems to be complicated can you maybe propose a way to make it easier within these constraints? – nohillside Aug 12 at 5:21
  • I see your point. It is not that using .bash_profile is complicated. It is that the way the files is invoked is complicated. I'll update. – Leo Aug 12 at 5:27
  • Calling .bashrc from .bash_profile is recommended in the GNU bash manual - otherwise how do you set variables etc that you need both in login shells and non intereactive ones? – Mark Aug 14 at 6:53
  • Every terminal emulator that I use on different OS's has an option to run new windows as a login shell, xterm and Xfce to name just two. – fd0 Aug 14 at 11:03

You must log in to answer this question.

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