Source: Shell Configuration · Zsh Mac · 2024
Zsh shell configuration for Mac Terminal. How to configure shell and set environment variables in .zshrc
and .zprofile
zsh files. A short complete guide.
If you need to set the $PATH
environment variable, see the guide Mac PATH for instructions.
Before you get started
You’ll need a terminal application to configure the shell. Apple includes the Mac terminal but I prefer Warp Terminal. Warp is an easy-to-use terminal application, with AI assistance to help you learn and remember terminal commands. Download Warp Terminal now; it’s FREE and worth a try.
What is the shell?
Zsh (the Z shell) is a program that runs in the Mac Terminal that interprets Unix commands and gives us the command line interface. The shell contains several basic commands such as cd
(change directory). Other commands are external to the shell and implemented as executable programs provided by Apple and stored in the file system, typically in directories like /bin
or /usr/bin
. For example, when you run ls
, the shell looks for the ls
file in these directories and runs it as a program.
Why configure the shell
You’ll need to configure the shell to set environment variables which are essential to make programs available or change program behavior. You may also want to configure other shell features, such as aliases or a custom prompt, just for convenience.
The $PATH
environment variable determines the directories the shell searches for executable files. It is the environment variable that almost everyone needs to set. See the guide Mac PATH for instructions.
You might set an alias if you have difficulty remembering the options you prefer for a command. For example, set the alias list
for ls -lag
.
Check the shell
Zsh is the default shell program on newer Macs. The original Bourne shell (sh
) and other shells (Zsh, Bash, Fish, Ksh, Tcsh) are similar for a beginner. They differ when it comes to more complex scripting for automation and customization aspects. Prior to macOS 10.15 Catalina, the Bash shell was the default in Mac Terminal.
Open the Terminal application and check which shell program is running in your terminal:
$ echo $SHELL
/bin/zsh
Don’t type the first $
(that’s just a prompt to let you know you are in the console). The second $
is needed as a variable symbol.
If you see /bin/bash
you should update to Zsh by running chsh -s /bin/zsh
.
Environment variables
Environment variables are default settings obtained at login or when launching the shell. Environment variables are set in the shell’s configuration files. They set preferences for software behavior, including paths to directories (for example, the PATH
setting). Environment variables are key-value pairs composed of a variable name and one or more values. Here is an example: EDITOR="nano"
.
Viewing environment variables
Environment variables are designed to be used by the system and applications. Ordinarily they are hidden to users. They are set when the Mac Terminal is launched so they operate invisibly.
You can view an environment variable with the echo
command. You must add the $
character to the beginning of the variable name to indicate you want to see the variable.
$ echo $LANG
en_US.UTF-8
You can see a list of all the current environment variables with the env
command. Try it:
$ env
.
.
.
Temporary or permanent changes
You can change an environment variable temporarily using the export
command, so it is in effect only in the current shell session. For example, to set the command line editor, you can enter and view the $EDITOR
environment variable. Notice the variable name needs no $
when it is set but needs the $
to view with echo
.
$ export EDITOR="/usr/bin/nano"
$ echo $EDITOR
/usr/bin/nano
If you close the terminal or start a new session, the $EDITOR
variable will revert to its previous setting or default value. A temporary setting is useful for testing or learning but it can be frustrating if you want to use it permanently.
To set an environment variable permanently, so it becomes a default for all future shell sessions, you must edit the shell’s configuration files.
.zshrc and .zprofile shell config files
These are the two zsh shell initialization files that are commonly used for configuration:
~/.zprofile
(Zsh Profile): This file is ideal for commands that should be executed only when the terminal window is opened, such as setting the$PATH
environment variable.~/.zshrc
(Zsh Run Control): This file is best for adjusting the appearance and behavior of the shell, such as setting command aliases and adjusting the shell prompt.
The article .zshrc or .zprofile explains the differences.
Don’t be confused by ~/.zshrc
(with a tilde) or .zshrc
(without a tilde). The tilde just means the .zshrc
file is found in your user home directory. The tilde ~/
is a Unix abbreviation for your home directory.
However, if you are a user on a new Mac, you won’t find these files.
Create the .zshrc and .zprofile zsh files
By default, the ~/.zshrc
and ~/.zprofile
zsh files do not exist for a user on a new Mac, even if you launch the zsh shell. You’ll need to manually create the files in your home directory to properly configure your development environment.
Check if the files already exist.
$ ls ~/.zprofile
$ ls ~/.zshrc
You can create new files from the command line with the touch
command:
$ touch ~/.zprofile
$ touch ~/.zshrc
After you create the files, you can edit them. If you run the touch
command on an existing file, the timestamp will be updated but the file will not be overwritten or deleted.
Displaying hidden files
The ~/.zshrc
and ~/.zprofile
files are hidden so you won’t see them in the macOS Finder file browser. It’s easy to hide a file; just use a dot as the first character in the file name. You can list all files, including hidden files, with the ls -lag
command in the Terminal.
In the macOS Finder (the file browser), you can enter Command + Shift + . to show hidden files. If you want to hide those files again, you can enter the same keystrokes.
You can force the Mac to always display hidden files by entering the following command in the Terminal application:
$ defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder
Hidden files will appear in gray in the Finder window.
Edit the zsh configuration files
You can use TextEdit, the default macOS graphical text editor, to edit the shell configuration files. Don’t use a word processor like Microsoft Word because it will introduce hidden formatting codes that are not suitable for plain text files.
You can open either file in TextEdit from the Mac Terminal:
$ open -e ~/.zprofile
$ open -e ~/.zshrc
You also can use the command line editors nano
or vim
to edit the shell configuration files. Nano is easier for beginners (use ctrl-X to quit); Vim can be frustrating for beginners (tip: use :q
to quit). If you haven’t used Nano or Vim before, just use TextEdit.
Reset the shell session
Close and reopen the Terminal window to pick up the changes to the configuration file. Alternatively (this is easier), you can use the source
command to reset the shell environment:
$ source ~/.zprofile
$ source ~/.zshrc
The source
command reads and executes a shell script file, resetting the shell environment.
What’s next
My mac.install.guide is a trusted source of installation guides for professional developers. Take a look at the Mac Install Guide home page for tips and trends and see what to install next.