Better Bash 4 & Completions on OSX

OS X ships with an outdated version of Bash as its default shell. The preinstalled version dates back to 2007, yes - nearly a decade behind.

If you want to use an updated version of Bash and Bash Completions in your shell, you can install them by using Homebrew, a package manager for OS X.

Installing via Homebrew

Open your terminal and enter this command:

brew install bash

Now we’ll append our desired (Bash 4) shell’s path to a file of whitelisted system shells, and then change the system shell for our user.

echo '/usr/local/bin/bash' | sudo tee -a /etc/shells;
chsh -s /usr/local/bin/bash;

If you don’t want to modify your system shell, you can use iTerm’s profile manager to use your brewed version

 iTerm.app -> Preferences -> Profiles -> General -> Command -> Click on "Command" and paste "/usr/local/bin/bash --login"

Just paste /usr/local/bin/bash and relaunch your default profile.

Testing New Bash

after you reload your profile, try

echo $BASH_VERSION

If you see something like 4.3.42(1)-release you’re good to go. The OSX binary is likely going to be ~ 3.2.57(1)-release

Better Bash Completions

Now that you’ve installed Bash 4 and your terminal sources it when you open a new profile, you can use bash_completions2 as it’s not compatible with the OSX shipped binary.

brew tap homebrew/versions
brew install bash-completion2

After you installed pay attention to the Caveatssection because you’ll need to add the following to your ~/.bash_profile

if [ -f $(brew --prefix)/share/bash-completion/bash_completion ]; then
    . $(brew --prefix)/share/bash-completion/bash_completion
 fi

In my dotfiles, I actually use:

# Add tab completion for bash completion 2
if which brew > /dev/null && [ -f "$(brew --prefix)/share/bash-completion/bash_completion" ]; then
  source "$(brew --prefix)/share/bash-completion/bash_completion";
elif [ -f /etc/bash_completion ]; then
  source /etc/bash_completion;
fi;

Now we can tab to autocomplete a range of commands, and pretty much any brew binary you have installed, ie:

$ git checkout
FETCH_HEAD      HEAD            ORIG_HEAD       master          origin/HEAD     origin/master

Bash Brothers Forever

Bash Brothers

Show Comments