Tuesday, 2 June 2020

How To Upgrade Your Bash Version On MacOS?

GNU Bash is a powerful shell, unfortunately most operating systems distribution don't provide you with the latest version which may prevent you to take advantage of the great new features that came with Bash 4 and 5. Also, running an outdated bash version probably expose you to some major vulnerabilities. For example, MacOS comes with Bash 3.x which is quite limiting and lack key features like associative arrays (dictionaries), improved auto-completion, better Posix conformance, etc. This post cover simple steps to upgrade bash on MacOS.

πŸ‘‰ Update: Find out more about Bash version 5 with my post What's New in GNU Bash 5?.

Check Your Current Bash Version

First and foremost, you need to know which version you are currently running as a defualt shell using the below commands.

[me@me-macOS: ~]$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

[me@me-macOS: ~]$ echo ${BASH_VERSION}
3.2.57(1)-release

Upgrade with Homebrew

If you are not yet using Homebrew, start doing so. It is a great package management for MacOS which will simplify a lot of the steps for you. Simply install it with this command:

[me@me-macOS: ~]$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then, upgrade, install, and reload bash.

[me@me-macOS: ~]$ brew upgrade
Updating Homebrew...

[me@me-macOS: ~]$ brew install bash
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
No changes to formulae.

==> Downloading https://homebrew.bintray.com/bottles/bash-5.0.11.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/01/0...
######################################################################## 100.0%
==> Pouring bash-5.0.11.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/bash/5.0.11: 150 files, 9.4MB

Reload and Verify

To reload bash, we can simply use exec as we saw in a previous post. Then, check your version again.

[me@me-macOS: ~]$ exec bash
[me@me-macOS: ~]$ bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

⚠️ Be careful: Your old bash 3 version would still be installed in /bin/bash while the brew version would be in /usr/local/bin/bash. You can check which version you are using with echo $SHELL or which bash.

Change Your Default Bash

πŸ‘‰ Note that the newest versions of macOS (Starting at Catalina) now uses zsh as a default instead of bash. Though, bash version 3 is still installed and available. To find out which macOS version youa re running, check out my post How to Find which Mac OS version you are running?.

To go a step further, you will want to make your new shell with Bash version 5 to be your user's default shell. First, you will need to update the list of permitted shells by adding the bash brew version into /private/etc/shells.

[me@me-macOS: ~]$ echo $(brew --prefix)/bin/bash | sudo tee -a /private/etc/shells
/usr/local/bin/bash

[me@me-macOS: ~]$ cat /private/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
/usr/local/bin/bash

Finally, you will eed to update your user's shell with chpass.

[me@me-macOS: ~]$ sudo chpass -s /usr/local/bin/bash johndoe
Changing shell for johndoe.

Alternatively, instead of using chpass, you can go to the  Menu > System Preferences... > Users & Groups. Unlock the pane, control click on your user to select Advanced Options..., then update the Login shell to /usr/local/bin/bash.


No comments:

Post a Comment

Merge All Finder Windows into a Single Tabbed Window in Mac OS X

  Finder Tabs are one of the better Mavericks improvements brought to the Mac OS X Finder in years, letting you use the file system in a sin...