当在bash中运行脚本时,我必须在开始时编写./:

$ ./manage.py syncdb

如果我不这样做,我得到一个错误消息:

$ manage.py syncdb
-bash: manage.py: command not found

这是什么原因呢?我想。是当前文件夹的别名,因此这两个调用应该是等效的。

我也不明白为什么在运行应用程序时不需要./,例如:

user:/home/user$ cd /usr/bin
user:/usr/bin$ git

(运行时不需要。/)


当前回答

When you include the '.' you are essentially giving the "full path" to the executable bash script, so your shell does not need to check your PATH variable. Without the '.' your shell will look in your PATH variable (which you can see by running echo $PATH to see if the command you typed lives in any of the folders on your PATH. If it doesn't (as is the case with manage.py) it says it can't find the file. It is considered bad practice to include the current directory on your PATH, which is explained reasonably well here: http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html

其他回答

这个问题已经有了一些很棒的答案,但我想补充一点,如果你的可执行文件在PATH上,当你运行时,你会得到非常不同的输出

./executable

和你逃跑后得到的那些人

executable

(假设您遇到了一个错误消息,而不是另一个),那么问题可能是您的机器上有两个不同版本的可执行文件:一个在路径上,另一个不在路径上。

通过运行

这可执行

and

whereis executable

它解决了我的问题……我有三个版本的可执行文件,其中只有一个是针对环境正确编译的。

所有这些都有很好的答案,是的,这只适用于在当前目录上运行时,除非你包含绝对路径。请参阅下面的示例。

此外,当我在子文件夹tmp2 (/tmp/tmp2)上有命令时,(点-斜杠)对我来说是有意义的,它使用(双点-斜杠)。

示例:

[fifiip-172-31-17-12 tmp]$ ./StackO.sh

Hello Stack Overflow

[fifi@ip-172-31-17-12 tmp]$ /tmp/StackO.sh

Hello Stack Overflow

[fifi@ip-172-31-17-12 tmp]$ mkdir tmp2

[fifi@ip-172-31-17-12 tmp]$ cd tmp2/

[fifi@ip-172-31-17-12 tmp2]$ ../StackO.sh

Hello Stack Overflow

在*nix上,与Windows不同,当前目录通常不在$PATH变量中。因此,在执行命令时不搜索当前目录。你不需要。/来运行应用程序,因为这些应用程序在你的$PATH;它们很可能在/bin或/usr/bin中。

当shell查找$PATH环境变量以查找您的脚本时,将无法在主目录中找到您的脚本。

./表示“在当前目录中查找我的脚本,而不是查看$PATH中指定的所有目录”。

When you include the '.' you are essentially giving the "full path" to the executable bash script, so your shell does not need to check your PATH variable. Without the '.' your shell will look in your PATH variable (which you can see by running echo $PATH to see if the command you typed lives in any of the folders on your PATH. If it doesn't (as is the case with manage.py) it says it can't find the file. It is considered bad practice to include the current directory on your PATH, which is explained reasonably well here: http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html