DEV Community

Brian P. Hogan
Brian P. Hogan

Posted on • Originally published at smallsharpsoftwaretools.com

12 1 1

Use the Modern Bash Shell on macOS

macOS ships with an older version of the Bash shell, because newer versions use a license that makes it more difficult for Apple to integrate into their OS. In macOS Catalina, Apple changed the default shell to ZSH for this reason.

But by using Homebrew, you can install the latest version of Bash and use its features.

First, check your current Bash version:

$ echo $BASH_VERSION
Enter fullscreen mode Exit fullscreen mode

If you see a version like the following, you're running a very old version:

3.2.57(1)-release
Enter fullscreen mode Exit fullscreen mode

Ensure you have Homebrew installed, which you can do by following Installing Homebrew.

Then use Homebrew to install Bash:

$ brew install bash
Enter fullscreen mode Exit fullscreen mode

To see where Homebrew installed Bash, run the following command:

$ ls "$(brew --prefix)/bin/bash" 
Enter fullscreen mode Exit fullscreen mode

You'll see a result like this:

/usr/local/bin/bash
Enter fullscreen mode Exit fullscreen mode

Next, add the path to Homebrew's version of Bash to the /etc/shells file. Display the contents of the file to ensure that it's not already listed:

$ cat /etc/shells
Enter fullscreen mode Exit fullscreen mode

You'll see something like this:

# 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
Enter fullscreen mode Exit fullscreen mode

Use the following command to add the newer version of Bash to the /etc/shells file:

$ echo "$(brew --prefix)/bin/bash" | sudo tee -a /etc/shells;
Enter fullscreen mode Exit fullscreen mode

Since the file is owned by the administrator, you can't redirect text using standard redirection, but you can work around it by using tee to send output to a file as it's run with sudo. The -a flag appends text rather than overwriting the existing file.

Ensure it was added:

$ cat /etc/shells
Enter fullscreen mode Exit fullscreen mode

The new shell will be listed at the end of the file:

# 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
Enter fullscreen mode Exit fullscreen mode

Finally, use the chsh command to change the shell for your user:

$ chsh -s "$(brew --prefix)/bin/bash"
Enter fullscreen mode Exit fullscreen mode

You'll be promped for your password. Enter it and your shell will change:

Changing shell for brianhogan.
Password for brianhogan:
Enter fullscreen mode Exit fullscreen mode

The current shell doesn't change, so open a new Terminal and use the following command to check your Bash version:

$ echo $BASH_VERSION
5.0.11(1)-release
Enter fullscreen mode Exit fullscreen mode

You now have the latest version installed.

To change back to the system-installed version of Bash, use chsh -s /bin/bash.

Conclusion

You've changed your shell to the latest version of Bash, and you know how to change it back to the default. You can use the same approach to install the latest version of ZSH, or other shells like Fish. Install the shell, add the path to its binary to /etc/shells, and use chsh to switch the shell.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (3)

Collapse
 
erikweibust profile image
Erik Weibust

Any killer features or fixes in the new version of bash that warrants the upgrade? Not that I'm opposed to shiny new things. Just curious.

Collapse
 
bphogan profile image
Brian P. Hogan

One of the bigger issues you run into is that some things that have worked on Ubuntu 16 and higher won't be compatible with your Mac. They use Bash 4, while macOS uses Bash 3.

Check out tiscase.edu/php/chet/bash/NEWS to learn what's happening in each version.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay