Set Mac terminal shell with proper GNU ‘bash’ version v4

5 minute read   Updated:

The bash version that comes with Mac OS Sierra is still the old v3 dated back in 2007.

# Mac Sierra
$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.12.4
BuildVersion:	16E195
# Bash version that comes with Mac OS
$ which bash; bash --version
/bin/bash
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.

GNU bash v4 has been introduced since 2009 that includes many new features, e.g. one of which is the shell option globstar ** that we used to recursively traverse directory hierarchy.

GET GNU bash

We can get it with homebrew.

# Search for 'bash' and looks like it's not installed on our Mac
$ brew search bash
apm-bash-completion        bash-git-prompt            calabash
bash                       bash-preexec               checkbashisms
bash-completion ✔          bashdb
bash-completion@2          bashish

# Check its version
$ brew info bash
bash: stable 4.4.12 (bottled), HEAD
Bourne-Again SHell, a UNIX command interpreter
https://www.gnu.org/software/bash/
Not installed

homebrew GNU bash looks good with version v4.4.12. Let’s install:

$ brew install bash

Check if our GNU bash is now the default one:

$ which -a bash
/usr/local/bin/bash
/bin/bash

Make sure /usr/local/bin/bash is at the top of the list. If it isn’t and /bin/bash is at the top, edit your .bashrc or .bash_profile to have the proper $PATH setting:

export PATH=/usr/local/bin:$PATH

Our terminal bash should now be the GNU bash v4.4.12:

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

And we’re done.

Issue

… well, not quite. Even though most of everything we do now on our terminal will run through the GNU bash thanks to the proper $PATH pointing to the correct version, our shell environment is still not fully set up properly.

E.g. we’ll run to an error when trying to change a shell option:

# Enable 'globstar' option for bash shell v4+
$ shopt -s globstar
-bash: shopt: globstar: invalid shell option name

Our version is bash v4.4.12 and it supports the globstar option. Why did we get the error when setting it? It’s simply because even though our $PATH points to the correct GNU bash, there are other things going on when the terminal loads up our shell environment. So besides $PATH, we need to make sure our shell environment is loaded up properly as well.

We have 2 places to do that on our Mac.

OPTION 1: terminal DEFAULT

terminal is an Mac app that wraps our shell. By default terminal will run /bin/bash when it opens, but we can configure it to use GNU bash instead.

Go to Terminal > Preferences > Shell opens with, and choose the 2nd option Command (complete path) with the value /usr/local/bin/bash as follows:

This tells the terminal app to run our Command GNU bash instead of the Default login shell /bin/bash when it opens.

Completely quit and then re-open terminal so it uses /usr/local/bin/bash, we can then change the shell option without any issue:

# Enable 'globstar' option for bash shell v4+
$ shopt -s globstar

# Check the option
$ shopt | grep globstar
26:globstar       	on

# Do fancy things with globstar **
# e.g. search for all .png files in all folders
$ ls **/*.png

OPTION 2: DEFAULT LOGIN SHELL

For most people, the terminal option should be sufficient. However there are cases when you execute some shell script and even though your $PATH and terminal is set properly to use GNU bash, somehow the shell script complains about the default shell /bin/bash, where does it come from?

It comes from the shell environment variable $SHELL that is set by Mac OS for your user. Even with $PATH and terminal set to use /usr/local/bin/bash, $SHELL still has the /bin/bash value:

$ echo $SHELL
/bin/bash

The discrepancy between our $PATH/terminal and $SHELL:

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

$ $SHELL --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.

As you may have guessed, in our terminal’s preferences, the Shell opens with’s default option Default login shell was actually using the value of $SHELL.

We can change the value of $SHELL with the change shell command chsh.

First-attempt to change default shell to use GNU bash

$ chsh -s /usr/local/bin/bash
Changing shell for <user>.
Password for <user>: 
chsh: /usr/local/bin/bash: non-standard shell

chsh reported back with the error non-standard shell, that is because we tried to set the GNU bash as the default login shell but it’s not in the list of valid shells as specified in /etc/shells:

$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

Second-attempt to change default shell to use GNU bash

Add GNU bash to the list of acceptable shells:

# Add 'GNU bash' as root
$ sudo -i
$ echo "/usr/local/bin/bash" >> /etc/shells
$ exit

# Double check the list of shells
$ cat /etc/shells
...
/usr/local/bin/bash

/usr/local/bin/bash is now in the acceptable shell list, let’s change our default shell again:

$ chsh -s /usr/local/bin/bash
Changing shell for <user>.
Password for <user>: 

Check that our shell is now the GNU bash:

$ echo $SHELL
/usr/local/bin/bash

Another way to check if our $SHELL has been property set is to run the chsh command without any argument which will present an editable view of our user configuration:

$ chsh 
Changing shell for <user>.
Password for <user>: 

# Changing user information for <user>.
# Use "passwd" to change the password.
##
# Open Directory: /Local/Default
##
Shell: /usr/local/bin/bash
Full Name: <user>
Office Location:
Office Phone:
Home Phone:

We can then exit without saving once we’ve verified that our shell is /usr/local/bin/bash.

You should logout and re-login for Mac to properly update your shell environment.

RECAP

  1. Install GNU bash with homebrew
  2. Configure proper $PATH
  3. Configure terminal to properly load the shell environment with either:
    • Choosing Command (complete path) with value /usr/local/bin/bash
    • Choosing Default login shell after setting $SHELL with chsh to use /usr/local/bin/bash

Updated 5/16/17 for Mac Sierra 10.12.4

Leave a Comment