“ls: not found” after running “read PATH”

The variable name PATH is already reserved for a different purpose: It lists all the possible locations searched to find commands not built into the shell.

ls is such a command. Thus, when you change the value of PATH, you change the way the shell tries to look for the ls executable; unless the new value of PATH includes a directory with a ls executable in it, any further attempts to run ls (or other commands not built into the shell) will fail.

Instead, use a different variable name — ideally, including at least one lower-case character, to avoid conflict with (all-uppercase) builtins and environment variables.


Thus, one corrected form might be:

#!/system/bin/sh 
echo "enter directory for listing"
IFS= read -r path

ls -R -- "$path" > list.txt

Note that the -R is moved before the "$path" in this case — while GNU systems will allow optional arguments to be after positional arguments, many older UNIX systems will only treat flags (like -R) as valid if they’re found before the first non-flag/option argument.

Leave a Comment