Altoplace

How to Configure the Bash Shell on macOS

A bash shell window.
A bash shell window on a MacBook Air.

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.

Table of Contents

Before We Start

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

We will be using Homebrew to install the lastest 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
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

## If not, do:

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

## or

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

$ brew install bash

$ sudo -i                                                                                 
Password:
root#

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

## Or

root# echo /opt/homebrew/bin/bash >> /etc/shells
root# exit
$

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.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/bash

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

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

$ echo $SHELL
/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.

george@macair1:~
$ cat .profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

PS1="\e[0;32m\u@\h\e[m:\w\n$ "
george@macair1:~
$ cat .bashrc
PATH=$HOME/bin:/opt/homebrew/bin:$PATH

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

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

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

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

# 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'

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.

george@macair1:~
$ mkdir tmpdir
george@macair1:~
$ cd tmpdir
george@macair1:~/tmpdir
$ touch .dotfile tmpfile
george@macair1:~/tmpdir
$ l
tmpfile
george@macair1:~/tmpdir
$ la
.dotfile  tmpfile
george@macair1:~/tmpdir
$ 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
george@macair1:~/tmpdir
$ 

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

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.

If this post was useful, please consider buying me a coffee at Ko-Fi. This will help me with my website expenses. Thanks!

Menu