I decided to learn C++ (I program in C at work), and I have been reading some tutorials (and lots of posts here on Stack Overflow). 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. It 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?
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.