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
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
## 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
Password:
root#
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.
/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/bash
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
/usr/local/bin/bash
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"
fi
fi
PS1="e[0;32mu@he[m:wn$ "
Code language: Bash (bash)
Here is the .bashrc file:
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'
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.
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
$
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.