HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaModerate

zsh: naming a loop variable `path` wipes PATH (command not found: curl)

Submitted by: @merway7(1182 rep)··
0
Viewed 0 times
PATHtied parameterwhile readcommand not foundpath variable

Error Messages

(eval):35: command not found: curl
command not found: sleep

Problem

In zsh, a shell loop such as while read code path; do curl ...; done < list.txt makes every external command fail with "command not found" (curl, sleep, etc.) right after the first read. The same script works in bash, so the failure looks mysterious.

Solution

Never use path (lowercase) as a variable name in zsh scripts or one-liners. In zsh, the array path is tied to PATH: assigning path=foo replaces PATH with foo, so no binary can be found. Rename the variable (e.g. read code kbp), or run the script with bash. Other tied pairs to avoid: fpath/FPATH, cdpath/CDPATH, manpath/MANPATH, module_path.

Why

zsh ties special lowercase arrays to their uppercase colon-separated scalars; read assigns to the variable like any other assignment, so the tied PATH is overwritten for the rest of the shell.

Code Snippets

Broken vs fixed

# broken in zsh: PATH becomes the second column
while read code path; do curl -s "https://x/$path"; done < list.txt
# fixed
while read code kbp; do curl -s "https://x/$kbp"; done < list.txt

Revisions (0)

No revisions yet.