Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Add New Path to PATH Command Line (Windows & Linux)

· 6 min read · Home Tech

In this article
  1. What Is the PATH Variable?
  2. Why Add a Directory to PATH?
  3. How to Add a Path Temporarily (Current Session Only)
  4. How to Add a Path Permanently
  5. How to Choose Between Appending and Prepending
  6. Common Mistakes and How to Avoid Them
  7. How to Verify Your Change
  8. Frequently Asked Questions

To add a new path to the PATH environment variable from the command line, use set PATH=%PATH%;C:\your\new\path in Windows Command Prompt (temporary) or export PATH=$PATH:/your/new/path in Linux/macOS (temporary). For a permanent change, use setx PATH "%PATH%;C:\your\new\path" in Windows or add the export line to your shell’s startup file (e.g., ~/.bashrc) in Linux/macOS.

What Is the PATH Variable?

The PATH environment variable is a list of directories that your operating system searches when you type a command without its full location. When you type python or git, the system looks through each directory in PATH, in order, until it finds an executable with that name. If the directory isn’t in PATH, you’ll get a “command not found” error (Linux/macOS) or “’python’ is not recognized” (Windows).

PATH is stored as a single string with directories separated by a semicolon (;) on Windows and a colon (:) on Linux/macOS. For example, on Windows it might look like C:\Windows\System32;C:\Program Files\Python312. On Linux, /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin.

Why Add a Directory to PATH?

Adding a directory to PATH lets you run its programs from anywhere in the terminal without typing the full path. Common reasons include:

  • Installing a command-line tool (e.g., Python, Node.js, FFmpeg) that doesn’t automatically add itself.
  • Running your own scripts or compiled binaries from a custom folder.
  • Using a package manager’s bin directory (like ~/.local/bin on Linux).
  • Switching between multiple versions of the same software.

Without PATH, you’d have to type C:\Users\Name\AppData\Local\Programs\Python\Python312\python.exe every time—which is tedious and error-prone.

How to Add a Path Temporarily (Current Session Only)

A temporary change lasts only for the current terminal window. It’s useful for testing or one-off tasks, and it doesn’t modify any system files.

Windows (Command Prompt)

set PATH=%PATH%;C:\your\new\path

This appends the new directory to the existing PATH. To verify, type echo %PATH% and check that the new entry appears.

Windows (PowerShell)

$env:Path += ";C:\your\new\path"

PowerShell uses the same semicolon separator but the syntax is different.

Linux/macOS (Bash, Zsh, etc.)

export PATH="$PATH:/your/new/path"

Again, this appends to the existing PATH. Check with echo $PATH.

Important: The change is lost when you close the terminal. If you open a new terminal, the old PATH is restored.

How to Add a Path Permanently

For a permanent change, you need to modify the system or user configuration so that every new terminal session picks up the new directory.

Windows (setx)

setx PATH "%PATH%;C:\your\new\path"

The setx command writes the value to the registry, making it permanent for future sessions. However, it does not affect the current terminal. Also, be careful: setx truncates values longer than 1024 characters, and it can overwrite the PATH if you’re not careful. Always include the %PATH% in the new value, as shown.

Alternative: Use the GUI (System Properties → Environment Variables), but the command line is faster for scripting.

Linux/macOS (Shell Configuration)

Add the export line to your shell’s startup file. Common files:

  • ~/.bashrc (Bash on Linux)
  • ~/.zshrc (Zsh on macOS and Linux)
  • ~/.profile (login shells)

Open the file with a text editor, add the line, then run source ~/.bashrc (or the corresponding file) to apply it immediately. The change will persist across reboots.

For system-wide changes (all users), edit /etc/environment or /etc/profile, but that requires administrative privileges and is rarely necessary.

How to Choose Between Appending and Prepending

When you add a directory, you can either append it to the end of PATH (%PATH%;new) or prepend it to the beginning (new;%PATH%). The order matters because the system searches directories in order. If you have two versions of a program, the one in the first directory wins.

  • Append (most common): The new directory is searched last. Use this when you don’t want to override existing commands.
  • Prepend: The new directory is searched first. Use this when you want to override a system default with your own version (e.g., a newer Python).

For example, on Linux, if you want your custom python to take precedence, use export PATH="/home/me/bin:$PATH".

Common Mistakes and How to Avoid Them

Several pitfalls trip up users when modifying PATH:

  • Forgetting the separator: On Windows, use a semicolon; on Linux/macOS, a colon. Using the wrong one will merge directories into a single invalid path.
  • Overwriting PATH entirely: If you write set PATH=C:\new instead of set PATH=%PATH%;C:\new, you lose all existing directories, breaking many commands.
  • Spaces in paths: On Windows, if a path contains spaces (e.g., C:\Program Files), you don’t need quotes in set because it’s a single string, but in some contexts you may need to quote the entire value. In Linux, spaces are rare but require escaping or quoting in the export command.
  • Using setx with %PATH% expansion: When you run setx PATH "%PATH%;...", the %PATH% is expanded to the current value, which can be long and may include variables like %SystemRoot% that get expanded literally. This is usually fine, but it can cause issues if the expanded value contains special characters.
  • Not restarting the terminal: After a permanent change, you must open a new terminal (or source the config file) for the change to take effect.

How to Verify Your Change

After adding a path, confirm it worked:

  1. Open a new terminal window (or run source ~/.bashrc on Linux/macOS).
  2. Type echo %PATH% (Windows) or echo $PATH (Linux/macOS) and check that your new directory is listed.
  3. Try running a command from that directory without specifying the full path. For example, if you added C:\tools and it contains mytool.exe, type mytool and see if it executes.

If you get “command not found,” double-check the spelling, the separator, and whether the directory actually contains an executable file.

Frequently Asked Questions

How do I add a path to PATH in Windows 11?

The command-line methods are the same as in Windows 10: use set for temporary or setx for permanent. You can also use the GUI: press Win+X, select System, then Advanced system settings → Environment Variables, and edit the Path variable there.

What is the difference between user PATH and system PATH?

Windows has two PATH variables: one for the system (all users) and one for the current user. The system PATH is set in the registry under HKLM and requires admin rights to modify. The user PATH is under HKCU and can be changed without admin. When you run setx PATH, it modifies the user PATH by default (unless you specify /M for system). On Linux, there’s only one PATH variable per session, but system-wide defaults are in /etc/environment.

Why does my PATH change not work after I close and reopen the terminal?

That means you only made a temporary change. For a permanent effect, you must use setx on Windows or add the export line to your shell startup file on Linux/macOS. Remember to open a new terminal after the change.

Can I add multiple paths at once?

Yes. Separate each directory with a semicolon (Windows) or colon (Linux/macOS). For example: set PATH=%PATH%;C:\one;C:\two or export PATH="$PATH:/one:/two".

What does “command not found” mean after adding a path?

It usually means the directory you added is incorrect, the executable is not in that directory, or the file isn’t marked as executable (on Linux/macOS). Check the directory spelling, ensure the file exists, and on Linux run ls -l to see if it has execute permission (-rwxr-xr-x). If not, use chmod +x filename.

Is it safe to edit PATH?

Yes, as long as you don’t remove existing directories. Always append or prepend to the current value, and avoid deleting entries you didn’t add. If you accidentally break PATH, you can restore it from the System Properties GUI or by resetting the variable to a known-good value (e.g., set PATH=C:\Windows\System32;C:\Windows on Windows).