为什么在可执行文件或脚本名称之前需要 ./(点斜线)才能在 bash 中运行它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6331075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:38:40  来源:igfitidea点击:

Why do you need ./ (dot-slash) before executable or script name to run it in bash?

bashshellunixcommand-line

提问by Dan Abramov

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

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

$ ./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

(which runs without ./)

(它没有运行./

回答by cnicutar

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

因为在 Unix 上,通常当前目录不在$PATH.

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

当您键入命令时,shell 将查找由PATH变量指定的目录列表。当前目录不在该列表中。

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

该列表中没有当前目录的原因是安全性。

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

假设您是 root 用户并进入另一个用户的目录并键入sl而不是ls. 如果当前目录在 中PATH,shell 将尝试执行该sl目录中的程序(因为没有其他sl程序)。该sl程序可能是恶意的。

It works with ./because POSIX specifiesthat a command name that contain a /will be used as a filename directly, suppressing a search in $PATH. You could have used full path for the exact same effect, but ./is shorter and easier to write.

它可以工作,./因为POSIX 指定包含 a 的命令名称/将直接用作文件名,禁止在$PATH. 您可以使用完整路径以获得完全相同的效果,但./更短且更易于编写。

EDIT

编辑

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

sl部分只是一个例子。中的目录PATH按顺序搜索,并在匹配时执行该程序。因此,根据PATH外观,键入普通命令可能足以或可能不足以在当前目录中运行程序。

回答by neuro

When bash interprets the command line, it looks for commands in locations described in the environment variable $PATH. To see it type:

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

echo $PATH

You will have some paths separated by colons. As you will see the current path .is usually not in $PATH. So Bash cannot find your command if it is in the current directory. You can change it by having:

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

PATH=$PATH:.

This line adds the current directory in $PATHso you can do:

这一行添加了当前目录,$PATH以便您可以执行以下操作:

manage.py syncdb

It is notrecommended as it has security issue, plus you can have weird behaviours, as .varies upon the directory you are in :)

这是推荐,因为它有安全问题,再加上你可以有奇怪的行为,因为.当你在目录因人而异:)

Avoid:

避免:

PATH=.:$PATH

As you can “mask” some standard command and open the door to security breach :)

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

Just my two cents.

只有我的两分钱。

回答by mdm

Your script, when in your home directory will not be found when the shell looks at the $PATHenvironment variable to find your script.

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

The ./says 'look in the current directory for my script rather than looking at all the directories specified in $PATH'.

./说“在当前目录下找我的剧本,而不是看着都在指定的目录$PATH”。

回答by Mark Drago

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 $PATHto 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 脚本提供“完整路径”,因此您的 shell 不需要检查您的 PATH 变量。没有'.' 您的 shell 将查看您的 PATH 变量(您可以通过运行echo $PATH查看您输入的命令是否位于您的 PATH 上的任何文件夹中。如果不是(就像 manage.py 的情况一样),它会说找不到文件。将当前目录包含在 PATH 中被认为是不好的做法,这里解释得相当好:http: //www.faqs.org/faqs/unix-faq/faq/part2/section- 13.html

回答by Anomie

On *nix, unlike Windows, the current directory is usually not in your $PATHvariable. So the current directory is not searched when executing commands. You don't need ./for running applications because these applications arein your $PATH; most likely they are in /binor /usr/bin.

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

回答by KR_Henninger

This question already has some awesome answers, but I wanted to add that, if your executable is on the PATH, and you get very different outputs when you run

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

./executable

to the ones you get if you run

如果你跑步,你得到的人

executable

(let's say you run into error messages with the one and not the other), then the problem could be that you have two different versions of the executable on your machine: one on the path, and the other not.

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

Check this by running

通过运行检查这一点

which executable

哪个可执行文件

and

whereis executable

It fixed my issues...I had three versions of the executable, only one of which was compiled correctly for the environment.

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

回答by Gayan Hewa

When the script is not in the Path its required to do so. For more info read http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html

当脚本不在 Path 中时,它需要这样做。有关更多信息,请阅读http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html

回答by FIFI

All has great answer on the question, and yes this is only applicable when running it on the current directory not unless you include the absolute path. See my samples below.

所有人都对这个问题有很好的答案,是的,这仅适用于在当前目录上运行它时,除非您包含绝对路径。请参阅下面的示例。

Also, the (dot-slash) made sense to me when I've the command on the child folder tmp2 (/tmp/tmp2) and it uses (double dot-slash).

此外,当我在子文件夹 tmp2 (/tmp/tmp2) 上使用命令并使用(双点斜线)时,(点斜线)对我来说很有意义。

SAMPLE:

样本:

[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