tags:

views:

691

answers:

7

I decided to learn c++ (I program in c at work) and I have been reading some tutorials (and lots of posts here on SO). Ok, so I typed in the standard c++ "hello word", compiled with gcc on my ubuntu machine as "test".

Then I tried to run it by typing "test" and hitting enter. Nothing. Turns out I must run it with "./test". Ok,fine, I'll do that from now on. But why? The "./" just says that what I should run is in the current directory... Is the current directory not always part of the PATH when the os is searching for something to run? Can I make it so?

A: 

Not having '.' (the current directory) in the PATH is a minor security measure. You could always add it in if you'd like, though it's not a best practice.

Harper Shelby
+5  A: 

You can add "." to your PATH, but that won't help you in this case - "test" is a shell built in.

Paul Tomblin
Yes, I remember now. I have stepped in this trap before. So not only double but TRIPLE wtf on my part. Great.
c0m4
+3  A: 

Unfortunately, there is a Unix command called "test"...

Corey Trager
+9  A: 

Yes, the current directory is not part of your PATH. You don't want it to be, because then you could be in a directory that had a malicious program you didn't know about that you run.

What if you were used to running /usr/bin/grep, but you happened to be in a directory that a Bad Person put a malicious copy of grep in, and this time you run grep, and you're running grep out of the current directory, rather than /usr/bin/grep.

You certainly can add ./ to your PATH in your ~/.profile or ~/.bash_profile, but I don't recommend it.

And if it makes you feel any better, I had the same frustration 15 years ago when I started using Unix-like systems.

Andy Lester
That made me feel better :-)
c0m4
@Andy - may want to add that "test" is a normal *nix utility :)
warren
@Andy, @warren - more important than "normal *nix utility", it's a shell built-in, which means that even if you put "." at the front of your path, it will use the build-in instead.
Paul Tomblin
FWIW: traditionally multi-user systems (VMX, UNIX, Plan9) don't have '.' in the PATH, but single-user systems (CP/M and MS-DOS) do.
Javier
A: 

In case it's not clear, this is an aspect where Windows differs from Unix/Linux. On Windows, the current directory is implicitly in the path.

David M. Karr
+1  A: 

If there's a command line script you regularly run, you could set up a command line alias to rid yourself of the need to type ./ each time.

John McCollum
+1  A: 

Even if the current directory is at the very beginning of the $PATH, 'test' still won't run it on (most?) shells, as 'test' is a shell built-in command.

Chris Dodd