简体   繁体   English

在Ubuntu中使用./运行二进制文件

[英]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). 我决定学习C ++ (我在C中编写程序),我一直在阅读一些教程(以及Stack Overflow上的大量帖子)。 OK, so I typed in the standard C++ "hello word", compiled with GCC on my Ubuntu machine as "test". 好的,所以我输入标准的C ++“hello word”,在我的Ubuntu机器上用GCC编译为“test”。

Then I tried to run it by typing "test" and hitting enter. 然后我尝试通过输入“test”并按Enter键来运行它。 Nothing. 没有。 It turns out I must run it with "./test". 事实证明我必须用“./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? “./”只是说我应该运行的是当前目录...当操作系统搜索要运行的东西时,当前目录不总是PATH的一部分吗? Can I make it so? 我可以这样做吗?

Yes, the current directory is not part of your PATH. 是的,当前目录不是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. 如果你习惯于运行/ usr / bin / grep,但是你碰巧在一个目录中,那个坏人把grep的恶意副本放进去,这次你运行grep,然后你运行了grep当前目录,而不是/ usr / bin / grep。

You certainly can add ./ to your PATH in your ~/.profile or ~/.bash_profile, but I don't recommend it. 你当然可以在你的〜/ .profile或〜/ .bash_profile中添加./到你的PATH,但我不推荐它。

And if it makes you feel any better, I had the same frustration 15 years ago when I started using Unix-like systems. 如果它让你感觉更好,15年前当我开始使用类Unix系统时,我也有同样的挫败感。

You can add "." 你可以加 ”。” to your PATH, but that won't help you in this case - "test" is a shell built in. 到你的PATH,但在这种情况下这对你没有帮助 - “test”是一个内置的shell。

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

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

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

Not having '.' 没有 '。' (the current directory) in the PATH is a minor security measure. PATH中的(当前目录)是次要安全措施。 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. 如果不清楚,这是Windows与Unix / Linux不同的一个方面。 On Windows, the current directory is implicitly in the path. 在Windows上,当前目录隐含在路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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