Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run binary with ./ in Ubuntu

Tags:

linux

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?

like image 642
c0m4 Avatar asked Nov 30 '22 20:11

c0m4


2 Answers

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.

like image 126
Andy Lester Avatar answered Dec 06 '22 19:12

Andy Lester


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

like image 25
Paul Tomblin Avatar answered Dec 06 '22 19:12

Paul Tomblin