Page 1 of 1

$PATH and scripts

Posted: Sun Sep 30, 2012 8:12 pm
by ToddFerrante
I'm about to rip my hair out I'm so frustrated. I just flashed back to a fresh Raspberrian, and updated. I've created the classic "hello world" script in /usr/local/bin, called "test". When I check my path with "echo $PATH", that directory is in my path. "pwd" shows that I'm in that directory. I used the nano editor to create the script, and chmod'd it to be executable. If I type "./test", it works properly. If I type "test", it does nothing. No error message, just another command line prompt. I must be doing some simple wrong, but I can't figure it out. Any ideas?

Todd F.

Re: $PATH and scripts

Posted: Sun Sep 30, 2012 8:33 pm
by itimpi
Isn't test a reserved word in the shell?

Re: $PATH and scripts

Posted: Sun Sep 30, 2012 8:45 pm
by ToddFerrante
That was the problem. I renamed it "tftest" and it worked properly. I knew there had to be a simple answer.
Todd F.

Re: $PATH and scripts

Posted: Sun Sep 30, 2012 9:35 pm
by jojopi
itimpi wrote:Isn't test a reserved word in the shell?
Technically it is not a reserved word, but it is a builtin shell command in bash. You can disable a builtin with "enable -n test" and then "test" will run the first version from $PATH, probably /usr/local/bin. However, it is still a bad idea to call a program /usr/local/bin/test because there is also a standard binary /usr/bin/test, compatible with the builtin, and overriding it in /usr/local/bin could potentially break programs that rely on it.

Re: $PATH and scripts

Posted: Sun Sep 30, 2012 9:36 pm
by billb
The which command is useful for things like this ... it will tell you the location of which program/command that would run had you entered it by itself.

In this case:

Code: Select all

which test
Would say:

/usr/bin/test

For more info see:

Code: Select all

man which
(yeah I know, a sandwich is a sandwich... :P)

Re: $PATH and scripts

Posted: Sun Sep 30, 2012 9:46 pm
by jojopi
billb wrote:which test
Would say:
/usr/bin/test
Which, no pun intended, is completely wrong. "which" is an external command and knows nothing about shell builtins. It is pointless and should never be used. Instead, "type test" will tell you "test is a shell builtin" or, if disabled, "test is /usr/local/bin/test".

Re: $PATH and scripts

Posted: Sun Sep 30, 2012 9:56 pm
by billb
Thanks -- "type" looks much more useful than "which" :)

Re: $PATH and scripts

Posted: Sun Sep 30, 2012 10:01 pm
by Joe Schmoe
Since my fingers are used to typing 'which' or 'where', I usually alias one or both (on those rare occasions when I'm using bash as an interactive shell) to "type -a".