bash not finding executables on Catalina: why & how to fix? As I was typing the title, I saw a suggestion that this might be a duplicate of.  But the suggestion vanished before I could click on it.

'sh' is actually bash

sh: sleep: command not found
sh: dirname: command not found
sh: dirname: command not found
sh: sleep: command not found
sh: dirname: command not found
^C
sh-3.2# echo $path
/usr/local/bin /usr/bin /bin /usr/sbin /sbin
sh-3.2# echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
sh-3.2# which dirname
/usr/bin/dirname
sh-3.2# which sleep
/bin/sleep
# the command that got those error messages was:
sh-3.2# grep -v copy /tmp/tmp | while read PATH; do dirname "$(dirname "$PATH")"; sleep 1; done


All available updates installed. 10.15.2
This is due to use of the special variable PATH (in while read PATH). PATH is used by they system as a list of directories to find command executables; if it doesn't point to the usual binaries directories, the OS won't be able to find commands any more.

Solution: use a different (preferably lower- or mixed-case) name for the variable.

There are quite a few all-caps variable names with some sort of special meaning to the shell, operating system, or various commands themselves. To avoid stumbling into conflicts and unintended consequences like this, it's generally best to avoid all-caps variable names in shell scripts (unless you want the special meaning). In fact, according to the POSIX standard for environment variables:


  The name space of environment variable names containing lowercase
  letters is reserved for applications. Applications can define any
  environment variables with names from this name space without
  modifying the behavior of the standard utilities.