The PATH Variable
When you type git status, how does the shell know where the git program lives? It looks it up in the PATH
environment variable: a list of folders, searched in order. The first folder that contains a matching program
wins. If no folder has it, you get the famous error:
CMD: 'git' is not recognized as an internal or external command,
operable program or batch file.
PowerShell: git: The term 'git' is not recognized as a name of a cmdlet, function, script file, or executable program.
bash: bash: git: command not found
zsh: zsh: command not found: git"Command not found" after installing a tool almost always means its folder is not on PATH yet, or the terminal was opened before the installer changed PATH. Close and reopen the terminal first.
How the lookup works
PATH = C:\Windows\system32 ; C:\Program Files\Git\cmd ; C:\Users\ada\tools
| | |
type "git" -> 1. look here (no) -> 2. look here: git.exe! -> run it (3 is never checked)| Windows | Linux / macOS | |
|---|---|---|
| Separator | ; |
: |
| Variable name | Path (case-insensitive) |
PATH |
| Which files count | extensions listed in PATHEXT (.COM;.EXE;.BAT;.CMD;...) |
files with the execute permission |
| Current folder searched? | CMD: yes, first. PowerShell: no (.\tool) |
no (./tool) |
Not searching the current folder is a security feature: a file named ls dropped into a download folder can't
hijack your command.
Windows (CMD)
Show the folders one per line, find a program, and add your own folder for this session:
C:\Users\ada> echo %PATH%
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Git\cmd;C:\Users\ada\AppData\Local\Microsoft\WindowsApps
C:\Users\ada> where git
C:\Program Files\Git\cmd\git.exe(sample output — yours will differ). Adding a folder for the current session is a normal set:
@echo off
mkdir tools
>tools\greet.bat echo @echo Hello from a tool on PATH
set "PATH=%CD%\tools;%PATH%"
call greet
for %%d in ("%PATH:;=" "%") do if /i "%%~d"=="%CD%\tools" echo found on PATH: %%~dHello from a tool on PATH
found on PATH: C:\Users\learner\tools
call is needed because running a batch file from another batch file otherwise never returns. The for line splits
PATH at every ; (a trick worth recognising when you read other people's scripts).
Windows (PowerShell)
New-Item -ItemType Directory tools | Out-Null
Set-Content tools/greet.ps1 '"Hello from a tool on PATH"'
$sep = [IO.Path]::PathSeparator
$env:PATH = (Join-Path $PWD "tools") + $sep + $env:PATH
greet.ps1
(Get-Command greet.ps1).Source
($env:PATH -split $sep)[0]Hello from a tool on PATH
/tmp/tools/greet.ps1
/tmp/tools
The playground runs PowerShell on Linux, so the paths use / and the separator is :;
[IO.Path]::PathSeparator picks the right one on each OS. On Windows, Get-Command git shows the full path of
the program, just like where (note: in PowerShell, where is an alias for Where-Object, so use where.exe).
Changing PATH permanently on Windows
Use Settings → System → About → Advanced system settings → Environment Variables, edit Path under "User
variables" and add one folder per line. Or from PowerShell:
PS C:\Users\ada> $old = [Environment]::GetEnvironmentVariable("Path", "User")
PS C:\Users\ada> [Environment]::SetEnvironmentVariable("Path", "$old;C:\Users\ada\tools", "User")Avoid setx PATH "%PATH%;...": it copies the combined system + user PATH into your user PATH and cuts values
longer than 1024 characters.
Linux & macOS (bash/zsh)
mkdir -p ~/bin
printf '#!/bin/sh\necho "Hello from a tool on PATH"\n' > ~/bin/greet
greet 2>/dev/null || echo "not found yet"
chmod +x ~/bin/greet
export PATH="$HOME/bin:$PATH"
greet
command -v greet
echo "$PATH" | tr ':' '\n' | head -n 1not found yet
Hello from a tool on PATH
/home/learner/bin/greet
/home/learner/bin
command -v name(orwhich name) shows which file will run;type -a namelists every match, in order.- Put your folder first (
$HOME/bin:$PATH) to override system tools, or last to only add new ones. - bash caches lookups; after moving a program,
hash -rclears the cache.
To make it permanent, add the export PATH=... line to ~/.bashrc (Linux) or ~/.zshrc / ~/.zprofile (macOS).
On macOS, Homebrew's installer asks you to add its brew shellenv line to ~/.zprofile; that is how
/opt/homebrew/bin gets onto your PATH. System-wide folders are listed in /etc/paths.
zsh: PATH as an array
zsh mirrors PATH in a lowercase array called path, which is easier to edit. typeset -U removes duplicates:
PATH=/usr/bin:/bin
typeset -U path
path=(~/bin $path /usr/bin)
print -l $path
print $PATH/home/learner/bin
/usr/bin
/bin
/home/learner/bin:/usr/bin:/bin
Comparison
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Show PATH | echo %PATH% |
$env:PATH -split ';' |
echo "$PATH" | tr ':' '\n' |
| Which program runs? | where git |
Get-Command git |
command -v git |
| Add a folder (session) | set "PATH=C:\tools;%PATH%" |
$env:PATH = "C:\tools;" + $env:PATH |
export PATH="$HOME/bin:$PATH" |
| Add permanently | Environment Variables dialog | SetEnvironmentVariable(..., "User") |
line in ~/.bashrc / ~/.zshrc |
| Run from current folder | tool |
.\tool |
./tool |
Learn more: Variables & Environment Variables, Bash Environment, Batch Environment.
Test yourself
You installed a tool, but the terminal still says 'command not found'. What is the first thing to try?
- Reinstall the OS
- Open a new terminal window so it reads the updated PATH
- Rename the tool
- Delete PATH
What separates folders in PATH on Linux and macOS?
- ;
- ,
- :
- |
Why must you type ./build.sh instead of build.sh in bash?
- bash only runs files with a dot
- The current folder is not on PATH, for security
- Scripts need a ./ prefix to be executable
- build.sh is a reserved name