在Linux shebang行中,/usr/bin/env是如何工作的?

时间:2021-10-27 23:13:00

I know shebang line like this:

我知道shebang line是这样的:

#!/bin/sh

but I found out I can also use shebang line like this:

但我发现我也可以用shebang line:

#!/usr/bin/env python3

This confuses me, can someone explain to me how Linux will process this one?

这让我困惑,谁能给我解释一下Linux将如何处理这个?

4 个解决方案

#1


3  

env is the name of a Unix program. If you read the manual (man env) you can see that one way to use it is env COMMAND, where in your case, COMMAND is python3.

env是Unix程序的名称。如果您阅读手册(man env),您可以看到使用它的一种方式是env命令,在您的例子中,命令是python3。

According to the manual, this will

根据手册,这将

Set each NAME to VALUE in the environment and run COMMAND.

将每个名称设置为环境中的值并运行命令。

Running env alone will show you what NAMEs and VALUEs are set:

仅运行env就会显示设置了哪些名称和值:

$ env
TERM=xterm-256color
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
…

Therefore, /usr/bin/env python3 is an instruction to set the PATH (as well as all the other NAME+VALUE pairs), and then run python3, using the first directory in the PATH contains the python3 executable.

因此,/usr/bin/env python3是一个指令,用于设置路径(以及所有其他名称+值对),然后运行python3,使用路径中的第一个目录包含python3可执行文件。

#2


0  

Newer *nix versions will resolve this the same way as the command which works.

更新的*nix版本将以与工作的命令相同的方式解决这个问题。

It looks in all directories which are set in the environmental variable $PATH, whereever it is set (global, in your .bashrc or other logon script or by hand), one by one and returns the first match.

它在环境变量$PATH中设置的所有目录中逐个查找,并返回第一个匹配项。

Important is, that some linux versions create alias files (aka symlinks), e.g. debian.

重要的是,一些linux版本创建了别名文件(即符号链接),例如debian。

Another note: the bash command alias overrides this behavior as it is performed first.

另一个注意事项:bash命令别名在首次执行时重写此行为。

#3


0  

Essentially like

本质上喜欢

tail -n +1 yourfile | /usr/bin/env python

#4


0  

#!/bin/sh

is only the path of the interpreter binary, whereas

只是解释器二进制的路径,而?

#!/usr/bin/env python3

has path of the interpreter passing python3 as optional argument to the #!/usr/bin/env interpreter

具有解释器的路径,该路径将python3作为可选参数传递给#!/usr/bin/env翻译

Please refer wiki for more info.

请参考维基了解更多信息。

#1


3  

env is the name of a Unix program. If you read the manual (man env) you can see that one way to use it is env COMMAND, where in your case, COMMAND is python3.

env是Unix程序的名称。如果您阅读手册(man env),您可以看到使用它的一种方式是env命令,在您的例子中,命令是python3。

According to the manual, this will

根据手册,这将

Set each NAME to VALUE in the environment and run COMMAND.

将每个名称设置为环境中的值并运行命令。

Running env alone will show you what NAMEs and VALUEs are set:

仅运行env就会显示设置了哪些名称和值:

$ env
TERM=xterm-256color
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
…

Therefore, /usr/bin/env python3 is an instruction to set the PATH (as well as all the other NAME+VALUE pairs), and then run python3, using the first directory in the PATH contains the python3 executable.

因此,/usr/bin/env python3是一个指令,用于设置路径(以及所有其他名称+值对),然后运行python3,使用路径中的第一个目录包含python3可执行文件。

#2


0  

Newer *nix versions will resolve this the same way as the command which works.

更新的*nix版本将以与工作的命令相同的方式解决这个问题。

It looks in all directories which are set in the environmental variable $PATH, whereever it is set (global, in your .bashrc or other logon script or by hand), one by one and returns the first match.

它在环境变量$PATH中设置的所有目录中逐个查找,并返回第一个匹配项。

Important is, that some linux versions create alias files (aka symlinks), e.g. debian.

重要的是,一些linux版本创建了别名文件(即符号链接),例如debian。

Another note: the bash command alias overrides this behavior as it is performed first.

另一个注意事项:bash命令别名在首次执行时重写此行为。

#3


0  

Essentially like

本质上喜欢

tail -n +1 yourfile | /usr/bin/env python

#4


0  

#!/bin/sh

is only the path of the interpreter binary, whereas

只是解释器二进制的路径,而?

#!/usr/bin/env python3

has path of the interpreter passing python3 as optional argument to the #!/usr/bin/env interpreter

具有解释器的路径,该路径将python3作为可选参数传递给#!/usr/bin/env翻译

Please refer wiki for more info.

请参考维基了解更多信息。