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. However, zsh is a very good shell to use, especially for interactive use.
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.
I am 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.
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
/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin/...
## If not, do:
$ 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).
If you are using zsh, update your PATH variable in your .zshrc
file.
Install the bash Executable
First install bash and then update /etc/shells
with the path to the newly installed bash version. This file contains the list of valid shells that a user may switch to. You must change to the root user to update this file.
$ brew install bash
$ sudo -i
Password:
mac1:~ root# echo /opt/homebrew/bin/bash >> /etc/shells
mac1:~ root# exit
logout
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
/opt/homebrew/bin/bash
Activate the Homebrew bash Shell
Do the following to switch to the new shell.
$ chsh -s /opt/homebrew/bin/bash
Changing shell for george.
Password for george:
Restart your terminal session. Then you can verify that your new shell is active:
$ echo $SHELL
/opt/homebrew/bin/bash
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 some 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
# user@system:~/user/working/directory
# $
PS1="\e[0;32m\u@\h\e[m:\w\n$ "
Here is the .bashrc file:
PATH=$HOME/bin:/opt/homebrew/bin:/opt/homebrew/sbin:$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 Apr 8 11:40 ./
drwxr-x---+ 54 george staff 1728 Apr 8 16:26 ../
-rw-r--r-- 1 george staff 0 Apr 8 11:40 .dotfile
-rw-r--r-- 1 george staff 0 Apr 8 11:40 tmpfile
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 comments are welcome! You can use your email address to Sign In (your email address is not publicly displayed), or you can use your Google or GitHub account. All comment data is hosted on Altoplace and is not tracked; please refer to the Remark42 Privacy Policy for more information.