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

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