简体   繁体   中英

Run binary with ./ in Ubuntu

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.

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

不幸的是,有一个名为“test”的Unix命令......

如果您经常运行命令行脚本,则可以设置命令行别名以消除每次键入./的需要。

即使当前目录位于$ PATH的最开头,'test'仍然不会在(大多数?)shell上运行它,因为'test'是一个shell内置命令。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM