As @William said in his answer, Apple does not provide bash 4 due to GPL restrictions. You can install bash 4+ however and also can make it your default shell (including for Terminal and iTerm2) by doing the following.
Install Bash 4 via Homebrew
First install the newer version of bash. There are various ways of doing that, I prefer Homebrew.
- Install Homebrew as described at http://brew.sh.
- Install bash using
brew install bash
.
Bash 4 is now available on your PATH (assuming Homebrew bin is on your path). However, it is not yet your default shell. You can find where it is located by running which bash
. In my case it is at /usr/local/bin/bash
.
Using Bash 4
Since it is on your PATH, you can start a Bash 4 session with just bash
or it can be used in scripts by using a Shebang.
For example, this will use a specific bash instance.
#!/usr/local/bin/bash
...your script...
This will use the first bash on the PATH.
#!/usr/bin/env bash
...your script...
You can also set the bash path for specific profiles in Terminal/iTerm2 using the steps described in @user136952's answer.
Making Bash 4 the default
As mentioned above, after installing Bash 4 is still not the default shell. To make bash the default you need to do two more steps.
First, add the Bash 4 path to your /etc/shells
file so that it is an allowed login shell. As described in /etc/shells, this file has the list of valid login shells. After adding the new bash path my /etc/shells
looks like the following:
# 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
/usr/local/bin/bash
Next we use chsh to make it your default shell. So any sessions for that user will use that shell. You can read more about this in Change the Shell in Mac OS X Terminal, but the actual command is very straightforward.
chsh -s /usr/local/bin/bash
Now the new bash is our default login shell. If you open Terminal or iTerm2 and run bash --version
you should see the new version. Note the "License GPLv3+" which is why Apple doesn't bundle it with macOS.
$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.6.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>