PK on Security & Privacy

About Me   -  Blog

Setting PATH for Linux, Mac, and Windows

Since I was tired of looking it up anew everytime I need to manipulate the PATH variable, I decided to write a guide for all operating systems on how to add a folder to the PATH environment variable. Here is a concise list of how to add directories to path for Linux, Mac, and Windows.

Linux

Suppose you want to add the directory /home/patrick/.local/bin
Open the terminal and look at (and potentially backup) the existing PATH:

$ echo $PATH

Add the new directory to the path:

$ export PATH=$PATH:/home/patrick/.local/bin

That's it!

Mac (temporary)

Suppose you want to add the directory /home/patrick/.local/bin
Open the terminal and look at (and potentially backup) the existing PATH:

% echo $PATH

Add the new directory to the path:

% export PATH=$PATH:/home/patrick/.local/bin

The PATH will reset after you logout.

Mac (permanent)

For current macOS versions this works via the zsh:
Copy the path to the /folder/program/script you want to add to the path and open zsh with any text editor you want.

% vi ~/.zshrc

If there is already a line export PATH=$PATH:... then append your previously copied entry to that with a colon (:), so like that:

export PATH=$PATH:/some/random/path:/home/patrick/.local/bin

if not, add a new line:

export PATH=$PATH:/home/patrick/.local/bin

You have to close and reopen the Terminal for changes to take effect.

Windows (temporary):

Windows is a bit tricky, as the PATH is truncated to 1024 characters. Be sure to backup your PATH before you manipulate it.
Open prompt: Win+R, cmd, Enter
Look at path and make a backup

C:\> echo %PATH% C:\> echo %PATH% > C:\PATH_BACKUP.txt

Temporarily add Folder D:\my\new\Folder to PATH

C:\> set PATH="%PATH%;D:\my\new\Folder"

The PATH will reset after you logout.
In case you want to restore your PATH simply copy the content of PATH_BACKUP.txt to the clipboard and paste it between the quotation marks (instead of OLD PATH)

C:\> set PATH="OLD PATH"

Windows (permanent)

A permanent change to PATH requires admin rights for the command prompt.
Search for CMD, right click and "Run as Administrator"
Again, don't forget to backup your PATH before you make any changes.

C:\> echo %PATH% > C:\PATH_BACKUP.txt

Permanently add Folder D:\my\new\Folder to PATH for all users

C:\> setx /M path "%PATH%;D:\my\new\Folder"

(ignore the /M if you just want to add it for the current user)
You need to restart the command prompt in order to see the changes.

-PK, 06.12.2022