7

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.

9
  • 1
    Don't use PATH you are blowing up the $PATH variable which is how the shell finds commands to run. In general don't use any ALL_CAPS variables. Those are "reserved" for shell/etc. usage. Also telling us what your actual error was would have been very helpful. Feb 3, 2015 at 23:22
  • 2
    @JONAS402, ...and Etan already told you why, and how to fix it. Feb 3, 2015 at 23:24
  • 1
    @JONAS402... rename your variable from PATH to path, and you're done. The PATH variable is used to determine where to look for executables; overwrite it, and the shell can no longer find ls. Feb 3, 2015 at 23:25
  • 1
    ...so, it's nothing about using "a variable", it's about that specific variable name. Feb 3, 2015 at 23:26
  • 1
    Yes, I figured out what error you were getting but my point was I shouldn't have had to figure it out. You should have included it as it was the point of your question. Feb 3, 2015 at 23:26

2 Answers 2

9

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.

2
  • Very good pieces of advice in here. Note that in this very case, $path also happens to be special in zsh (where it's an array tied to $PATH like in csh), so you'd get the same problem in zsh there (not when zsh is in sh emulation though). Nov 30, 2015 at 21:57
  • Also note that even GNU ls won't accept options after arguments if there's a POSIXLY_CORRECT variable in the environment. Nov 30, 2015 at 21:58
-1

I fixed it by resetting my iTerm 2. enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.