当在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

其他回答

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

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

echo $PATH

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

PATH=$PATH:.

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

manage.py syncdb

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

避免:

PATH=.:$PATH

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

这只是我的个人意见。

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

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

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

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