Macos – Migrating from bash to zsh for Mac OS Catalina

bashmacos catalinaoh-my-zshzsh

As some of you may know, after upgrading to Mac OS Catalina, Apple is prompting users to migrate to zsh as the default shell.

Now, there is a warning that comes up every time bash is opened. It can be disabled adding the line below to your ~/.bash_profile (for those interested).

export BASH_SILENCE_DEPRECATION_WARNING=1

However, I figure that many (including me) want to move on to zsh.

My current ~/.bash_profile looks like below:

# searches this directory for executables first
export PATH="/usr/local/bin:$PATH"

# jenv
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

# pyenv
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

# nodenv
export PATH="$HOME/.nodenv/bin:$PATH"
eval "$(nodenv init -)"

# node-build-definitions
export NODE_BUILD_DEFINITIONS="/usr/local/opt/node-build-update-defs/share/node-build"

# bash auto-completion
if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion    
fi

# git branch in prompt
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# bash profile theme
export PS1="\[\e[1;37m\]parthnaik:\[\033[33;1m\]\w\[\033[m\]\$(parse_git_branch) \n$ "
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

# firevault memory security
alias sleepsafe='sudo pmset -a destroyfvkeyonstandby 1 hibernatemode 25 standby 0 standbydelay 0'
alias sleepdefault='sudo pmset -a destroyfvkeyonstandby 0 hibernatemode 3 standby 1 standbydelay 10800'

# enable / disable captive portal
alias disablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool false'
alias enablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool true'

I was hoping someone more knowledgeable than me can help me understand what goes where since there seems to be some conflicting information online.

So far, from what I have read, here are the different suggestions I have seen:

  1. Copy pasting ~/.bash_profile to ~/.zshrc.
  2. Adding the following code at the bottom of ~/.zshrc:
if [ -f ~/.bash_profile ]; then 
    . ~/.bash_profile;
fi
  1. Creating a ~/.aliases file and a ~/.paths file and then sourcing/importing them to both ~/bash_profile as well as ~/.zshrc to maintain backward compatibility.

In addition to this, I have a .sh script that runs everyday automatically via command like:

sh script_name.sh

Should I change be changing this to use zsh like shown below? This should be the case if all .sh scripts with bash and zsh.

zsh script_name.sh

Looking for advice and best practises for migration although I know that any of the above would work in terms of functionality. Ideally, would like my theme, autocompletions and git branch settings (as shown in the ~/.bash_profile above) to work the same way as they do now.

For the theme, I know that there is a plugin called 'oh-my-zsh' available as well. Is this recommended to be installed?

Thank you for the help!

Best Answer

I decided to use the pure theme for zsh. My scripts work as normal, they just run via zsh now. Here is what my ~/.zshrc file looks like:

# PATHS AND ALIASES
# searches this directory for executables first
export PATH="/usr/local/bin:$PATH"

# jenv
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

# pyenv
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

# nodenv
export PATH="$HOME/.nodenv/bin:$PATH"
eval "$(nodenv init -)"

# node-build-definitions
export NODE_BUILD_DEFINITIONS="/usr/local/opt/node-build-update-defs/share/node-build"

# firevault memory security
alias sleepsafe='sudo pmset -a destroyfvkeyonstandby 1 hibernatemode 25 standby 0 standbydelay 0'
alias sleepdefault='sudo pmset -a destroyfvkeyonstandby 0 hibernatemode 3 standby 1 standbydelay 10800'

# enable / disable captive portal
alias disablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool false'
alias enablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool true'

# CLI SETTINGS
# enable the default zsh completions
autoload -Uz compinit && compinit

# set colors for displaying directories and files when the ls command is used
export LSCOLORS='GxFxCxDxBxegedabagaced'
export CLICOLOR=1

# theme
fpath+=("$HOME/.zsh/pure")
autoload -U promptinit && promptinit
prompt pure

# change the path color
zstyle :prompt:pure:path color white
Related Question