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

$ ./manage.py syncdb

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

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

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

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

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

(运行时不需要。/)


当前回答

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

其他回答

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

./executable

和你逃跑后得到的那些人

executable

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

通过运行

这可执行

and

whereis executable

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

当bash解释命令行时,它在环境变量$PATH中描述的位置中查找命令。要查看它键入:

echo $PATH

你会有一些用冒号分隔的路径。正如您将看到的当前路径。通常不在$PATH中。因此,如果您的命令在当前目录中,Bash就无法找到它。你可以通过以下方式更改:

PATH=$PATH:.

这一行添加了当前目录在$PATH中,所以你可以这样做:

manage.py syncdb

不建议使用,因为它有安全问题,而且你可能会有奇怪的行为。取决于你所在的目录:)

避免:

PATH=.:$PATH

因为你可以“屏蔽”一些标准命令,并打开安全漏洞的大门:)

这只是我的个人意见。

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

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

当脚本不在路径中时,需要这样做。欲了解更多信息,请阅读http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html