为什么你需要./(点斜杠)之前的脚本名称在bash运行呢? [英] Why do you need ./ (dot-slash) before script name to run it in bash?

查看:319
本文介绍了为什么你需要./(点斜杠)之前的脚本名称在bash运行呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当运行在bash脚本,我必须写 ./ 中的开头:

When running scripts in bash, I have to write ./ in the beginning:

$ ./manage.py syncdb

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

If I don't, I get an error message:

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

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

What is the reason for this? I thought . is an alias for current folder, and therefore these two calls should be equivalent.

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

I also don't understand why I don't need ./ when running applications, such as:

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

(运行无 ./

推荐答案

由于在Unix上,通常情况下,当前目录不在 $ PATH

Because on Unix, usually, the current directory is not in $PATH.

当你输入一个命令的shell查找的目录列表,由 PATH 变量指定。当前目录不在该列表。

When you type a command the shell looks up a list of directories, as specified by the PATH variable. The current directory is not in that list.

原因没有在名单上当前目录是安全的。

The reason for not having the current directory on that list is security.

假设你的根和进入其他用户的目录,然后键入 SL 而不是 LS 。如果当前目录是 PATH ,shell将尝试执行 SL 计划在该目录(因为没有其他的 SL 程序)。这 SL 程序可能是恶意的。

Let's say you're root and go into another user's directory and type sl instead of ls. If the current directory is in PATH, the shell will try to execute the sl program in that directory (since there is no other sl program). That sl program might be malicious.

修改

这是 SL 部分只是一个例子。在路径中的目录顺序检索,当匹配时所执行的程序。所以,这取决于如何 PATH 看起来,键入一个正常的命令可能会或可能不足以在当前目录运行程序。

That sl part was just an example. The directories in PATH are searched sequentially and when a match is made that program is executed. So, depending on how PATH looks, typing a normal command may or may not be enough to run the program in the current directory.

这篇关于为什么你需要./(点斜杠)之前的脚本名称在bash运行呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆