Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ls: not found" after running "read PATH"

I'm trying to write a little script to list a directory from a given variable. However, I can't run ls at all after reading my input into the variable PATH.

#!/system/bin/sh 
echo "enter directory for listing"
read "PATH"

ls "$PATH" -R > list.txt

This exits with:

ls: not found

...and writes nothing to list.txt.

like image 367
JONAS402 Avatar asked Feb 03 '15 23:02

JONAS402


1 Answers

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.

like image 194
Charles Duffy Avatar answered Oct 09 '22 23:10

Charles Duffy