Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Upgrading to Bash 4 on MacOS [Snippet]

DZone 's Guide to

Upgrading to Bash 4 on MacOS [Snippet]

Need to access those shiny features in Bash 4 from your Mac? Look at how to quickly and easily upgrade from Bash 3.

· Integration Zone ·
Free Resource

Incredibly, macOS Mojave comes with Bash 3.22 by default still. Apparently this is due to licensing reasons, however Bash 4 has lots of lovely features including associative arrays that I’d like use. Hence, after reading on the internet for a bit, I’ve installed it on my Mac. This turned out to require just 3 commands:

$ brew install bash
$ sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells' 
$ chsh -s /usr/local/bin/bash

Close the terminal and open a new one, which now has Bash 4!

Bash4

Using Bash 4 in Scripts

Any script that starts with #!/bin/bash will continue to use Bash 3, so we use env in the same way as we do with PHP, Ruby, or Python scripts and start our Bash script files with:

#!/usr/bin/env bash

This will now use Bash 4 when you run the script interactively.

Topics:
macos ,bash ,shell ,script ,upgrade ,integration tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}