Setting up Environment Variables in macOS Big Sur, macOS Mojave, macOS Catalina, macOS Big Sur, and macOS Monterey
Please note that starting with macOS Catalina (hence including macOS Big Sur), your Mac uses zsh as the default login shell and interactive shell. bash in the default shell in macOS Mojave and earlier.
Most stories are filled with unnecessary details. I will get straight to the point so you can get what you need and move on. So here we go:
Click on your computer name. Find .bash_profile. (google how to unhide files you can’t .bash_profile in the path starting with your computer name). Right click and edit with TextEdit. Save and close.
How to switch to a zsh profile and prompt
If you’re using a bash profile, such as to set environment variables, aliases, or path variables, you should switch to using a zsh equivalent. For example:
- .zprofile is equivalent to .bash_profile and runs at login, including over SSH
- .zshrc is equivalent to .bashrc and runs for each new Terminal session
If you’re using .profile (a POSIX-compliant profile), you can make zsh automatically read its settings by adding this command to .zprofile:
[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'
You can also move some settings from a bash profile to a zsh profile without modification. For example, to set environment variables:
zsh recognizes a different set of prompt specifiers than bash and has a cleaner syntax for specifying colorized output, eliminating the need to use complex ANSI escape sequences. For example, here’s the syntax for a default bash prompt from .bash_profile:
export PS1="\[\e[92;40m\]\h\[\e[m\]:\[\e[93m\]\W\[\e[m\] \\$ "
To convert that bash prompt to a zsh prompt when using .zprofile or .zshrc:
export PS1="%10F%m%f:%11F%1~%f \$ "
See the zsh man page for more details.
Test your shell scripts:
To test script compatibility with Bourne-compatible shells in macOS Catalina, you can change /var/select/sh to /bin/bash, /bin/dash, or /bin/zsh. If you change /var/select/sh to a shell other than bash, be aware that scripts that make use of bashisms may not work properly.
zsh can be made to emulate sh by executing the command
zsh --emulate sh.
Additional notes: zsh is highly compatible with the Bourne shell (sh) and mostly compatible with bash. More info on zsh and its command0line completion system can be obtained via man zsh in Terminal.
To change the default shell via Terminal : Enter
$ chsh -s path, where path is one of the shell paths listed in /etc/shells, such as /bin/zsh, /bin/bash, /bin/csh, /bin/dash, /bin/ksh, /bin/sh, or /bin/tcsh.
To use a different shell with changing the default:
- Open Terminal, then choose Terminal > Preferences.
- From the General pane, select ”Command (complete path).”
- In the field provided, enter one of the shell paths listed in /etc/shells, such as /bin/zsh, /bin/bash, /bin/csh, /bin/dash, /bin/ksh, /bin/sh, or /bin/tcsh.