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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
JONAS402
  • 591
  • 1
  • 9
  • 18
  • 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. – Etan Reisner Feb 03 '15 at 23:22
  • Ok cheers for the heads up all I get is "ls: not found" – JONAS402 Feb 03 '15 at 23:24
  • 2
    @JONAS402, ...and Etan already told you why, and how to fix it. – Charles Duffy Feb 03 '15 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`. – Charles Duffy Feb 03 '15 at 23:25
  • 1
    ...so, it's nothing about using "a variable", it's about **that specific variable name**. – Charles Duffy Feb 03 '15 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. – Etan Reisner Feb 03 '15 at 23:26
  • Cheers guys, changed PATH to path and it works! Sorry, i didn't think of including the error... Will remember for the future! – JONAS402 Feb 03 '15 at 23:29
  • 1
    I've tried to edit the question to be more useful as an example of how they should be asked. – Charles Duffy Feb 03 '15 at 23:29
  • Does this answer your question? [Getting "command not found" error in bash script](https://stackoverflow.com/questions/5642521/getting-command-not-found-error-in-bash-script) – user202729 Dec 06 '21 at 06:48

2 Answers2

8

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 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). – Stephane Chazelas Nov 30 '15 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. – Stephane Chazelas Nov 30 '15 at 21:58
-1

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

Jun Yin
  • 2,019
  • 3
  • 16
  • 18