Upgrade to bash 4 in Mac OS X

Bash 4 has some nice new features that are worth the effort of updating.

Install bash

Bash version can be queried with the --version flag:

$ bash --version

The actual installation is going to happen with HomeBrew, the OS X package manager, if you don’t have it, installed it with the following command:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Update homebrew packet database and install bash:

$ brew update && brew install bash

$ bash --version might show 4.x, but bash might be using 3.x still, it’s straightforward to test, the global variable $BASH_VERSION returns bash version.

Imagine a file:

# version-test.sh

Make it executable and run it:

$ ./version-test.sh

Seemingly it’s using the old bash still. The trick is the shebang on the first line, it’s pointing to the old bash. Change it to:

# version-test.sh

Now run it and it gives the 4 as a version.

Configure terminal to use it

# Add the new shell to the list of allowed shells
sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'
# Change to the new shell
chsh -s /usr/local/bin/bash 

Now close terminal and boot the machine.

Why bash 4?

It supports associative arrays, plus bunch of other stuff.

External reading

Here’s an in depth superuser thread on this.


  • Mark J. Reed says:

    You can use #!/usr/bin/env bash in the shebang line and it will use whichever bash comes first in your $PATH; whether you want that flexibility or prefer the predictability of the static shebang line is a personal choice.

    You can use the tee command in lieu of sudo bash -c command-with-redirects: sudo tee -a /etc/shells <<</usr/local/bin/bash

  • steve says:

    This totally screwed up my command line. I cannot do any npm installs. It notes node and npm versions but nothing more. If any one knows how to debug this I could see using 4, but if not it will totally stop development on your mac.

  • Please update all /usr/local to $(brew –prefix) for more compatibility

  • Slartibartfast says:

    No dice for me, still says 3.2, all the way

  • Dennis says:

    If you try all the above methods and it does’t work or still shows the old bash, just go to the terminal logo on the left bar close to the apple logo and click terminal –> preference –> General –> shell open with -> and chose default login shell or just copy and paste /usr/local/bin/bash in the command path.

    after installing the bash 4.4, make sure it install by typing bash in the terminal and it will take you to the bash 4.4 command.

    I hope this helps

  • Quinn says:

    Thanks for this post, and excellent blog + blog design in general. an inspiration :)

  • Ed R. says:

    Thanks for this article.

    At the end it says to boot the machine (I assume it means reboot).

    But a reboot is NOT needed (at least on my Mac running 10.13, not sure about older OS versions).

  • Working great here, thanks! Just what I needed

    Screenshot: https://cl.ly/470T080g1t1V

  • Anonymous says:

    Excellent tutorial worked great! Do I need to override the older bash version or delete it though? Or will the package manager (home-brew) take care of that for me if I run a command?

Club-Mate, the beverage → club-mate.fi