Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add a new entry to the PATH variable in ZSH on Mac OS in Linux
The PATH variable in ZSH determines where the shell looks for executable commands. By default, macOS Catalina and later versions don't include a .zshrc file, so we need to create one to customize our shell environment. This file contains configuration settings and environment variables that are loaded every time a new ZSH session starts.
Creating the .zshrc File
To create the .zshrc file, follow these steps −
Open Terminal
Type
touch ~/.zshrcto create the filePress Return
You can open and edit the .zshrc file from any directory using −
vi ~/.zshrc
Example .zshrc Content
immukul@192 linux-questions-code % cat ~/.zshrc export GOPATH=/Users/immukul/go_projects export NDHOME=/Users/immukul/Downloads export GOTRACEBACK=all export GOROOT=/usr/local/go export LC_CTYPE=C export PATH=/home/Systems export LANG=C
Note: The output may vary from machine to machine based on your existing configuration.
Method 1 − Using Echo Command
To add a new entry to the PATH variable without opening the file manually, use the echo command −
echo 'export PATH=~/some/path:$PATH' >> ~/.zshrc
This appends the export statement to your .zshrc file. The new path will be prepended to the existing PATH variable.
immukul@192 linux-questions-code % cat ~/.zshrc export GOPATH=/Users/immukul/go_projects export NDHOME=/Users/immukul/Downloads export GOTRACEBACK=all export GOROOT=/usr/local/go export LC_CTYPE=C export PATH=/home/Systems:~/some/path export LANG=C
Method 2 − Manual Editing
Alternatively, you can open the .zshrc file in a text editor and add the export command manually −
export PATH=/home/bin:$PATH
After adding this line to your .zshrc file, the PATH will include the new directory −
immukul@192 linux-questions-code % cat ~/.zshrc export GOPATH=/Users/immukul/go_projects export NDHOME=/Users/immukul/Downloads export GOTRACEBACK=all export GOROOT=/usr/local/go export LC_CTYPE=C export PATH=/home/Systems:~/some/path:/home/bin export LANG=C
Applying Changes
After modifying the .zshrc file, you need to reload it to make the changes effective in your current terminal session −
source ~/.zshrc
Alternatively, you can close and reopen your terminal, or start a new terminal session to automatically load the updated configuration.
Key Points
Path Order Matters: When using
$PATHat the end, new paths are searched firstPermanent Changes: Modifications to
.zshrcpersist across terminal sessionsMultiple Entries: Separate multiple paths with colons (
:)Tilde Expansion: Use
~for the home directory path
Conclusion
Adding entries to the PATH variable in ZSH involves creating or modifying the .zshrc file and using the export PATH command. Always remember to source the file or restart your terminal to apply the changes to your current session.
