How to Configure the Bash Shell on macOS

A bash shell window.

Overview

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 We Start

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:

 1$ echo $PATH
 2/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
 3
 4## If not, do:
 5
 6$ PATH=/usr/local/bin:$PATH
 7
 8## or
 9
10$ PATH=/opt/homebrew/bin:$PATH

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.

 1$ brew install bash
 2
 3$ sudo -i                                                                                 
 4Password:
 5root#
 6
 7root# echo /usr/local/bin/bash >> /etc/shells
 8
 9## Or
10
11root# echo /opt/homebrew/bin/bash >> /etc/shells
12root# exit
13$

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

 1$ cat /etc/shells
 2# List of acceptable shells for chpass(1).
 3# Ftpd will not allow users to connect who are not using
 4# one of these shells.
 5
 6/bin/bash
 7/bin/csh
 8/bin/dash
 9/bin/ksh
10/bin/sh
11/bin/tcsh
12/bin/zsh
13/usr/local/bin/bash

Activate the Homebrew bash Shell

Do the following to switch to the new shell.

1$ chsh -s /usr/local/bin/bash
2Changing shell for george.
3Password for george:
4
5## Or
6
7$ chsh -s /opt/homebrew/bin/bash

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

1$ echo $SHELL
2/usr/local/bin/bash

Let's Tweak Our .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:

1# if running bash
2if [ -n "$BASH_VERSION" ]; then
3    # include .bashrc if it exists
4    if [ -f "$HOME/.bashrc" ]; then
5        . "$HOME/.bashrc"
6    fi
7fi
8
9PS1="e[0;32mu@he[m:wn$ "

Here is the .bashrc file:

 1PATH=$HOME/bin:/opt/homebrew/bin:$PATH
 2
 3# If not running interactively, don't do anything
 4case $- in
 5    *i*) ;;
 6      *) return;;
 7esac
 8
 9# don't put duplicate lines or lines starting with space in the history.
10# See bash(1) for more options
11HISTCONTROL=ignoreboth
12
13# append to the history file, don't overwrite it
14shopt -s histappend
15
16# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
17HISTSIZE=1000
18HISTFILESIZE=2000
19
20# check the window size after each command and, if necessary,
21# update the values of LINES and COLUMNS.
22shopt -s checkwinsize
23
24alias l='ls -CFG'
25alias la='ls -AFG'
26alias ll='ls -alFG'

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.

 1george@macair1:~
 2$ mkdir tmpdir
 3george@macair1:~
 4$ cd tmpdir
 5george@macair1:~/tmpdir
 6$ touch .dotfile tmpfile
 7george@macair1:~/tmpdir
 8$ l
 9tmpfile
10george@macair1:~/tmpdir
11$ la
12.dotfile  tmpfile
13george@macair1:~/tmpdir
14$ ll
15total 0
16drwxr-xr-x   4 george  staff   128 Dec 30 08:56 ./
17drwxr-xr-x+ 35 george  staff  1120 Dec 30 08:55 ../
18-rw-r--r-- 1 george  staff     0 Dec 30 08:56 .dotfile
19-rw-r--r-- 1 george  staff     0 Dec 30 08:56 tmpfile
20george@macair1:~/tmpdir
21$ 

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:

1$ brew upgrade bash

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

Warning

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.