Upgrading Bash on MacOS

Words
155
Reading
1 min
Listen
Play
7y

Bash Logo
Tech Tuesday Time.

Apple doesn't support GPLv3 thus bash won't be updated due to licensing.

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


However homebrew has it as a normal package. Time to install it.

$ brew install bash


Now lets add it to the list of shells.

$ sudo echo /usr/local/bin/bash >> /etc/shells


I even rearranged it to make the list look neater.

$ 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
/usr/local/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh


Last but not least, lets change the default system shell using chsh

$ sudo chsh -s /usr/local/bin/bash


Verifying that the new path is updated in the apple user configuration database:

$ dscl . -read /Users/$USER UserShell
UserShell: /usr/local/bin/bash


All is looking good!

$ echo $BASH_VERSION
5.0.2(1)-release

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

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Bonus Hack - Bash auto-completion

What's the point of installing bash 4.x and later versions if you can't enjoy extra auto-completion.
Lets install the latest auto-completion v2 that is compatible with bash 4.x or later.

$ brew install bash-completion@2


Notice that during the installation you are advised to do the following:

==> bash-completion@2
Add the following to your ~/.bash_profile:
  [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"


I'm adding what the installer suggests to my .profile file. In your case it might be .bash_profile.

$ echo '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' >> .profile


Now type rsync -- [Tab] for example and see what happens. Enjoy!

TL;DR
1) brew install bash
2) sudo echo /usr/local/bin/bash >> /etc/shells
3) sudo chsh -s /usr/local/bin/bash
4) dscl . -read /Users/$USER UserShell
5) echo $BASH_VERSION


Bonus:

1) brew install bash-completion@2
2) echo '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' >> .profile
Upgrading Bash on MacOS | Ecency