The PATH environment variable in zsh defines where the shell looks for executable files. Setting this variable correctly allows you to run programs, scripts, and commands from anywhere within the terminal. Knowing how to modify the PATH is important for ensuring smooth execution of commands and tools located outside the default system paths.
You can modify the PATH variable either temporarily or permanently. A temporary modification applies only for the current terminal session, while a permanent change ensures that the directory is available in the PATH for all future terminal sessions. Modifying the .zshrc configuration file is necessary for permanent changes.
Follow the steps below to set the PATH environment variable in your zsh shell.
Steps to set PATH environment variable in zsh:
- Open the terminal.
- Display the current PATH variable.
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- Temporarily add a directory to the PATH for the current session.
$ export PATH=$PATH:/your/directory/path
Replace “/your/directory/path” with the actual directory you want to add to the PATH.
- To make the change permanent, open the .zshrc file in a text editor.
$ vim ~/.zshrc
- Add the following line to the end of the .zshrc file to append your directory to the PATH:
$ export PATH=$PATH:/your/directory/path
Ensure that the directories you add to the PATH contain valid executable files and are correctly spelled.
- Save and exit the .zshrc file.
- Apply the changes to the current session.
$ source ~/.zshrc
- Verify that the new directory has been successfully added to the PATH.
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/your/directory/path
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.