它在 linux 脚本中是什么意思?#!/usr/bin/python -tt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9050331/
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
What does it mean in linux scripts? #!/usr/bin/python -tt
提问by Abzac
I know that in the begining of .sh bash scripts is
我知道在 .sh bash 脚本的开头是
#!/bin/bash
which points to the command interpeter executable.
它指向命令 interpeter 可执行文件。
But during watching Google Python Class http://www.youtube.com/watch?v=tKTZoB2VjukI noticed that for python they use
但是在观看 Google Python Class http://www.youtube.com/watch?v=tKTZoB2Vjuk 时,我注意到他们使用了 Python
#!/usr/bin/python -tt
. Surfing the Internet I also have found such styles of this notation:
. 在网上冲浪我也发现了这种符号的这种风格:
#!/usr/local/bin/python
and even
乃至
#!/usr/bin/env python
.
.
So, I'm new with Python and I'm ordinary Linux user and I have a few questions about this "magic" line:
所以,我是 Python 新手,我是普通的 Linux 用户,我对这个“魔法”行有一些疑问:
- First of all, what is the right form of this line? and why?
- What does -tt key means in #!/usr/bin/python -tt ?
- What program is parsing this line in Linux?
- What syntax of this line for any script?
- Why this line is so necessary if each file have it's extension?
- And what about that in each computer interpreter for some kind of scripts will be stored in different place than in another? And script couldn't be run.
- 首先,这条线的正确形式是什么?为什么?
- -tt 键在 #!/usr/bin/python -tt 中是什么意思?
- 什么程序在 Linux 中解析这一行?
- 任何脚本的这一行的语法是什么?
- 如果每个文件都有它的扩展名,为什么这一行如此必要?
- 那么在每个计算机解释器中,某种脚本的存储位置与另一个位置不同呢?并且脚本无法运行。
It's really interesting to me. What's this line? Why this line? How to write this line? Why in such a way?...
这对我来说真的很有趣。这条线是什么?为什么是这条线?这行怎么写?为什么会这样?...
采纳答案by Marc B
Question #1) The line is called a shebang, and there's no right form that works universally. e.g.
问题#1)这条线被称为shebang,没有普遍适用的正确形式。例如
#!python
#!/usr/bin/python
#!/usr/local/bin/python
#!/usr/bin/python -t
are all valid/acceptable forms, but may not work on all systems:
都是有效/可接受的形式,但可能不适用于所有系统:
#!python
will work only if the python executable is somewhere in your shell's PATH
#!python
只有当 python 可执行文件在你的 shell 的 PATH 中时才会工作
#!/usr/bin/python
only works if the python binary is actually in /usr/bin
#!/usr/bin/python
仅当 python 二进制文件实际上在 /usr/bin 中时才有效
#!/usr/local/bin/python
also only works if python is in /usr/local/bin
#!/usr/local/bin/python
也仅当 python 在 /usr/local/bin 中时才有效
Question #2)
问题2)
#!/usr/bin/python -tt
is passing the -tt
option to python, as if you'd done:
#!/usr/bin/python -tt
正在将-tt
选项传递给 python,就好像你已经完成了:
$ python -t somescript.py
at the shell prompt. You can pass arbitary command line arguments to the interpreter on the shebang line.
在 shell 提示符下。您可以在 shebang 行上将任意命令行参数传递给解释器。
Question #3)
问题 #3)
The line is interpreted by the OS kernel and the shell you're currently using. The stuff after the #!
simply tells the OS which program should be fired up to "execute" the rest of the script.
该行由操作系统内核和您当前使用的 shell 解释。后面的内容#!
只是告诉操作系统应该启动哪个程序来“执行”脚本的其余部分。
Question #4)
问题 #4)
The script syntax depends on the language you're using. E.g. a PHP shell script must take the form of
脚本语法取决于您使用的语言。例如,PHP shell 脚本必须采用以下形式
#!/usr/bin/php
<?php
... php code here ...
A #!/usr/bin/perl
perl script must use Perl syntax, etc... If you put PHP code with a Perl shebang, you'll just have Perl barf up the script with syntax errors, as PHP code is NOTperl code
一个#!/usr/bin/perl
perl脚本必须使用Perl语法等..如果你把PHP代码与一个Perl家当,你只需要Perl的BARF了语法错误的脚本,如PHP代码是不是Perl代码
Question #5)
问题 #5)
Shebangs are for Unix systems, where file extensions were never really used to identify file types to the OS. A .c
file was understood to be a C language source code file, but that's merely a convention. You could put a Bash shell script into a .c
file, make it executable, and with the #!/bin/bash
shebang, it would execute as a Bash script.
Shebangs 适用于 Unix 系统,其中文件扩展名从未真正用于向操作系统识别文件类型。一个.c
文件被理解为是一个C语言源代码文件,但是这仅仅是一个惯例。您可以将 Bash shell 脚本放入一个.c
文件中,使其可执行,然后使用#!/bin/bash
shebang,它将作为 Bash 脚本执行。
Determining executable types by file extension is more of a Windows thing.
通过文件扩展名确定可执行类型更像是 Windows 的事情。
Question #6)
问题 #6)
That goes back the stuff in question #1 - if the shebang claims the interpreter is at some OTHER path than where it is, this particular script can't be executed until the shebang is fixed, or the interpreter is moved. Shebangs are very handy, but not infallible.
这可以追溯到问题 #1 中的内容 - 如果 shebang 声称解释器位于其他路径而不是它所在的位置,则在修复 shebang 或移动解释器之前无法执行此特定脚本。Shebangs 非常方便,但并非万无一失。
Thankfully, most interpreters are installed in fairly standard locations these days, so it'd be somewhat unusual to find (say) Perl installed at /some/wonky/weird/path
instead of /usr/bin
值得庆幸的是,如今大多数解释器都安装在相当标准的位置,因此找到(例如)Perl 安装在/some/wonky/weird/path
而不是/usr/bin
回答by Abhijeet Rastogi
From the manpage:
从联机帮助页:
-t Issue a warning when a source file mixes tabs and spaces for indentation in a way that makes it depend on the worth of a tab expressed in spaces. Issue an error when the option is given twice.
-t 当源文件混合制表符和空格以缩进时发出警告,这种方式使其依赖于以空格表示的制表符的价值。两次给出选项时发出错误。
- The right form of the line is the one you want to use.
- It's the interpreter that reads this line known as shebang. If you write a python script with first line as "#!/usr/bin/python" & invoke it using bash, it's the /bin/sh interpreter that reads first line and starts the proper interpreter.
- It's a shebang. The syntax of feature consists of the character sequence #!, i.e. the number sign and an exclamation point character
- File extensions are not relevant in linux generally. You can have a python script that doesn't have a .py extension.
- 该行的正确形式是您要使用的形式。
- 读取这一行的解释器称为shebang。如果您编写一个第一行为“#!/usr/bin/python”的 Python 脚本并使用 bash 调用它,则 /bin/sh 解释器会读取第一行并启动正确的解释器。
- 这是一个shebang。feature的语法由字符序列#!组成,即数字符号和感叹号字符
- 文件扩展名通常在 linux 中不相关。您可以拥有一个没有 .py 扩展名的 python 脚本。
For ex.
例如。
shadyabhi@archlinux ~ $ cat a
print "Hello World"
shadyabhi@archlinux ~ $ python2 a
Hello World
shadyabhi@archlinux ~ $
Even the shebangs are only necessary if you want to start a script using $./script as in this case you didn't mention the interpreter you want to use.
如果您想使用 $./script 启动脚本,即使是shebangs也是必需的,因为在这种情况下您没有提到要使用的解释器。
回答by 0xbadc0de
- #!/usr/bin/env python
- issue errors about inconsistent tab usage
- Kernel
- #!/path_to_the_interpreter or /usr/bin/env
- *nix does not check extensinon at all(except some DE could do that)
- This is why you should use #!/usr/bin/env
- #!/usr/bin/env python
- 发出有关选项卡使用不一致的错误
- 核心
- #!/path_to_the_interpreter 或 /usr/bin/env
- *nix 根本不检查扩展(除了某些 DE 可以这样做)
- 这就是为什么你应该使用 #!/usr/bin/env
More info at wiki
维基上的更多信息
回答by WeNeedAnswers
The different paths are to where the python interpreter has been installed. Different flavours of Linux install it in different places.
不同的路径是安装 python 解释器的位置。不同风格的 Linux 将它安装在不同的地方。
Linux doesn't care for extensions its a Windows thing.
Linux 不关心扩展,它是 Windows 的东西。
The bash session uses the line to call the correct interpreter for the script your running.
bash 会话使用该行为您运行的脚本调用正确的解释器。
The different places to where the files are stored, called and used are all based on defined places where files should be and located by software. Dev for devices, home for user stored area, bin for programs. But as time has gone by, different systems require different locations.
文件存储、调用和使用的不同位置都是基于软件定义的文件应位于和定位的位置。Dev 为设备,home 为用户存储区,bin 为程序。但随着时间的推移,不同的系统需要不同的位置。
I would suggest getting a book on Linux/Unix and learning the basics of the file system. It does help a lot.
我建议买一本关于 Linux/Unix 的书并学习文件系统的基础知识。它确实有很大帮助。
回答by Kevin
This is called a shebang. It tells the system that it should pass the file as an argument to the specified program instead of trying to execute it per se.
这被称为shebang。它告诉系统它应该将文件作为参数传递给指定的程序,而不是尝试执行它本身。
First of all, what is the right form of this line? and why?
首先,这条线的正确形式是什么?为什么?
The correct path is wherever your python interpreter is installed. The arguments (-tt
) will depend on what you want. Some people insist on #!/usr/bin/env
in case the interpreter happens to be elewhere.
正确的路径是安装了 python 解释器的地方。参数 ( -tt
) 将取决于您想要什么。有些人坚持#!/usr/bin/env
以防口译员碰巧在别处。
What does -tt key means in #!/usr/bin/python -tt ?
-tt 键在 #!/usr/bin/python -tt 中是什么意思?
I don't use python, so someone else will have to answer this.
我不使用 python,所以其他人将不得不回答这个问题。
When I launch any script in Linux (not exact Python script) what program parses and uses this line? I consider that it's not bash because even for bash scripts this line is needed.
当我在 Linux 中启动任何脚本(不是确切的 Python 脚本)时,什么程序会解析并使用这一行?我认为它不是 bash,因为即使对于 bash 脚本,这行也是需要的。
I've heard (and am pretty sure) it's the kernel. Even if it were bash, it would need the line to tell bash it is supposed to be a script it should interpret instead of passing to another program. /usr/bin/env
is a command that searches the PATH
for the specified argument and passes the script through the program it finds.
我听说(并且很确定)它是内核。即使它是 bash,它也需要一行告诉 bash 它应该是一个脚本,它应该解释而不是传递给另一个程序。/usr/bin/env
是搜索PATH
指定参数并通过它找到的程序传递脚本的命令。
What syntax of this line for any script? And what's the name of interpeter that parses it?
任何脚本的这一行的语法是什么?解析它的 interpeter 的名字是什么?
The syntax is the same as a command line, #!command arguments
, but command
has to be an absolute path, the PATH
doesn't get searched.
语法与命令行相同#!command arguments
,但command
必须是绝对路径,PATH
不会被搜索。
Why this line is so necessary if each file have it's extension?
如果每个文件都有它的扩展名,为什么这一行如此必要?
Extensions mean nothing in *nix. I could name a bash script script.pl
, script.exe
, or even script
without an extension. If the script has the right shebang line, it gets passed through the right interpreter, otherwise the kernel tries to execute it as an executable and fails. The system doesn't know about extensions. They're a convention for users, nothing more.
扩展在 *nix 中没有任何意义。我可以命名一个 bash 脚本script.pl
, script.exe
, 甚至script
没有扩展名。如果脚本有正确的 shebang 行,它将通过正确的解释器传递,否则内核尝试将其作为可执行文件执行并失败。系统不知道扩展。它们是用户的约定,仅此而已。
And what about that in each computer interpreter for some kind of scripts will be stored in different place than in another? And script couldn't be run.
那么在每个计算机解释器中,某种脚本的存储位置与另一个位置不同呢?并且脚本无法运行。
If I understand this correctly, you're saying different systems / distributions keep the interpreters in different places (e.g. /usr/bin/python
and /usr/local/bin/python
), and asking how the system knows which to use?
The answer is, it uses the one which is at the absolute path you gave it. This is actually a slight problem with executable scripts, and the reason why /usr/bin/env
has come into vogue. As I said, env
searches the PATH
for the correct interpreter, so as long as your system has a /usr/bin/env
, you're set, you don't need to look up or guarantee the location of the interpreter.
如果我理解正确,您是说不同的系统/发行版将解释器保留在不同的位置(例如/usr/bin/python
和/usr/local/bin/python
),并询问系统如何知道使用哪个?
答案是,它使用您提供的绝对路径中的那个。这实际上是可执行脚本的一个小问题,也是为什么/usr/bin/env
会流行起来的原因。正如我所说,env
搜索PATH
正确的解释器,所以只要你的系统有一个/usr/bin/env
,你就设置好了,你不需要查找或保证解释器的位置。