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:

  1. Open the terminal.
  2. Display the current PATH variable.
    $ echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  3. 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.

  4. To make the change permanent, open the .zshrc file in a text editor.
    $ vim ~/.zshrc
  5. 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.

  6. Save and exit the .zshrc file.
  7. Apply the changes to the current session.
    $ source ~/.zshrc
  8. 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
Discuss the article:

Comment anonymously. Login not required.