Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Under Arch USB iso, zsh, running the script ./test containing :

#!/bin/bash
PATH=$(dirname $0)
ls $PATH

returns

ls: command not found

Any idea how to fix this ?

EDIT : ls alone works but not when I'm adding $PATH

Edit : just realized from the comments that $PATH is an environment variable and I was replacing its value...

share|improve this question
    
You can try to make a ls using for f in *; do echo $f; done , and try to determine the ls location, by this for equivalent for ls. But i think its a path problem. – Luciano Andress Martini Jul 25 at 14:12
1  
Put echo $PATH in your script. If it doesn't contain /bin then add it. – 123 Jul 25 at 14:27
    
It is good to strip a program to its minimum before pasting here, but please ensure that it is no smaller (test that the smaller program still exhibits the fault). – richard Jul 25 at 14:51
up vote 3 down vote accepted

The variable PATH is a special one. There are a lot of special variables (and all are all capital, so easily avoided). PATH holds a list of directories to search for commands.

For fun and learning (though these are the same thing), type echo $PATH, outside of script, to see what it has in it.

To fix problem do not break PATH: use a different variable name (not all capitals).

share|improve this answer

Try /bin/ls, it seems that ls is not in the path as @123 has mentioned in the comment.

To add /bin to the PATH. Add in ~/.bashrc

export $PATH=$PATH:/bin
share|improve this answer
    
This question was correct, before more detail was added to question. – richard Jul 25 at 17:14

Try:

#!/bin/bash
PATH="$PATH:$(dirname $0)"
ls $PATH

This way you're adding $(dirname $0) to the PATH variable instead of replacing it. Or, if you don't want to edit the environment variable and want to use $(dirname $0) separately, use a different variable name.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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