Upgrade to bash 4 in Mac OS X
Bash 4 has some nice new features that are worth the effort of updating.
Bash version can be queried with the
$ bash --version 3.2.53(1)-release
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:
#!/bin/bash # version-test.sh echo $BASH_VERSION
Make it executable and run it:
$ ./version-test.sh 3.2.53(1)-release
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:
#!/usr/local/bin/bash # version-test.sh echo $BASH_VERSION
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.
Here’s an in depth superuser thread on this.