Bash on Mac

2 minute read Published:

Bash on Mac

The problem

Since macOS Catalina and later Apple has changed the default shell terminal to zsh. Most of your scripts based on bash will have incompatibility issues with zsh. You can switch to bash but the version that comes with macOS is pretty old. Let’s change that.

Assumptions

  • You are on a mac device running macOS 10.15 and later.

Install brew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install latest bash and make it your default login shell.

# install latest bash
brew install bash
# install newer bash completions
brew install bash-completion@2
# edit /etc/shells and include /usr/local/bin/bash at the end of the file
sudo vim /etc/shells# append /usr/local/bin/bash
# Change default shell for your mac terminal
chsh -s /usr/local/bin/bash
# Change login shell for your user
sudo chpass -s /usr/local/bin/bash <Your_username>

Append to your ~/.bash_profile

# GIT FUNCTIONS
 git_branch() {
     git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
 }
 # TERMINAL PROMPT
 PS1="\[\e[0;93m\]\u\[\e[m\]@"    # username
 PS1+="\[\e[0;92m\]\h\[\e[m\]" # hostname
 PS1+=" "    # space
 PS1+="\[\e[0;95m\]\W\[\e[m\]"    # current directory
 PS1+="\[\e[0;92m\]\$(git_branch)\[\e[m\]"    # current branch
 PS1+=" "    # space
 PS1+="$ "    # end prompt
 export PS1;
 export CLICOLOR=1
 export LSCOLORS=GxFxCxDxBxegedabagaced
 export PATH=$HOME/bin:/usr/local/bin:/usr/local/opt/python/libexec/bin:$HOME/.local/bin:$PATH
# bash v5 completions --->
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
# bash v5 completions ---|
# get rid of apple's bash deprecation warning
export BASH_SILENCE_DEPRECATION_WARNING=1