How to Configure the Bash Shell on macOS

Recent versions of macOS now use zsh as the default command line shell. I typically use the bash shell on Linux servers, so I prefer using it on my Mac. This post is going to describe how to switch from the zsh shell to the bash shell.

Before Starting

You should be familiar with using the macOS Terminal command line.

We will be using Homebrew to install the latest bash version. The version that comes with macOS is very old, and it’s not being updated because of licensing issues. Homebrew is very easy to install; please refer to their website for instructions.

The following procedure for installing and activating bash will vary slightly depending on whether you are using an Intel or Apple’s silicon processor. Homebrew stores Intel executables in /usr/local/bin and Apple silicon (e.g., M1 processor) executables in /opt/homebrew/bin.

You may need to update your PATH variable to ensure that the path for Homebrew executables is at the beginning of your PATH:

$ echo $PATH

## If not, do:

$ PATH=/usr/local/bin:$PATH

## or

$ PATH=/opt/homebrew/bin:$PATH
Code language: Shell Session (shell)

You can update your PATH at the beginning of your .bashrc file to make it permanent. After doing so, restart your terminal session (we’ll talk more about the .bashrc file).

Install the bash Executable

We will first install bash and then update /etc/shells with the path to our newly installed bash version. This file contains the list of valid shells that a user may switch to.

$ brew install bash

$ sudo -i                                                                                 

root# echo /usr/local/bin/bash >> /etc/shells

## Or

root# echo /opt/homebrew/bin/bash >> /etc/shells
root# exit
Code language: Shell Session (shell)

When sudo prompts you for a password, enter your user password. Verify that you have correctly updated /etc/shells.

$ 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.

Code language: Shell Session (shell)

Activate the Homebrew bash Shell

Do the following to switch to the new shell.

$ chsh -s /usr/local/bin/bash
Changing shell for george.
Password for george:

## Or

$ chsh -s /opt/homebrew/bin/bash
Code language: Shell Session (shell)

Restart your terminal session. Then you can verify that your new shell is active:

$ echo $SHELL
Code language: PHP (php)

Tweak the .profile and .bashrc Environment

Now that your bash shell is working, you will want to start customizing your shell environment. Here are some suggestions to get started with. The following .profile and .bashrc files will create a two-line shell prompt, command line aliases for the ls (list files) command, and set up come command history options.

Here is the .profile file:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"

PS1="e[0;32mu@he[m:wn$ "
Code language: Bash (bash)

Here is the .bashrc file:


# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

alias l='ls -CFG'
alias la='ls -AFG'
alias ll='ls -alFG'
Code language: Bash (bash)

Be sure to restart your terminal session after making any changes to your bash shell environment. Here are some examples for using the ls aliases.

$ mkdir tmpdir
$ cd tmpdir
$ touch .dotfile tmpfile
$ l
$ la
.dotfile  tmpfile
$ ll
total 0
drwxr-xr-x   4 george  staff   128 Dec 30 08:56 ./
drwxr-xr-x+ 35 george  staff  1120 Dec 30 08:55 ../
-rw-r--r-- 1 george  staff     0 Dec 30 08:56 .dotfile
-rw-r--r-- 1 george  staff     0 Dec 30 08:56 tmpfile
Code language: Shell Session (shell)

Final Thoughts

When a new Homebrew bash version becomes available (as noted by running brew update), be sure to only upgrade the bash package by doing:

$ brew upgrade bash
Code language: Shell Session (shell)

Then immediately restart your terminal session. Afterwards, you can upgrade any remaining brew packages.

Your new bash shell is dependent on your Homebrew environment. If you decided to delete your Homebrew installation, be sure to switch your shell back to Apple’s zsh (or other shell outside of the Homebrew path) before deleting Homebrew.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.