ToddFerrante
Posts: 19
Joined: Sun Sep 30, 2012 8:00 pm

$PATH and scripts

Sun Sep 30, 2012 8:12 pm

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.

itimpi
Posts: 1090
Joined: Sun Sep 25, 2011 11:44 am
Location: Potters Bar, United Kingdom
Contact: Website

Re: $PATH and scripts

Sun Sep 30, 2012 8:33 pm

Isn't test a reserved word in the shell?

ToddFerrante
Posts: 19
Joined: Sun Sep 30, 2012 8:00 pm

Re: $PATH and scripts

Sun Sep 30, 2012 8:45 pm

That was the problem. I renamed it "tftest" and it worked properly. I knew there had to be a simple answer.
Todd F.

User avatar
jojopi
Posts: 3086
Joined: Tue Oct 11, 2011 8:38 pm

Re: $PATH and scripts

Sun Sep 30, 2012 9:35 pm

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.

User avatar
billb
Posts: 177
Joined: Wed Sep 19, 2012 10:27 pm

Re: $PATH and scripts

Sun Sep 30, 2012 9:36 pm

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)

User avatar
jojopi
Posts: 3086
Joined: Tue Oct 11, 2011 8:38 pm

Re: $PATH and scripts

Sun Sep 30, 2012 9:46 pm

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".

User avatar
billb
Posts: 177
Joined: Wed Sep 19, 2012 10:27 pm

Re: $PATH and scripts

Sun Sep 30, 2012 9:56 pm

Thanks -- "type" looks much more useful than "which" :)

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: $PATH and scripts

Sun Sep 30, 2012 10:01 pm

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".
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Return to “Beginners”