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=tKTZoB2Vjuk I noticed that for python they use
但是在观看谷歌Python课程http://www.youtube.com/watch?我注意到他们用的是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 ?
- 在#中-tt键是什么意思?/usr/bin/python tt吗?
- What program is parsing this line in Linux?
- 什么程序正在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.
- 那么在每个计算机解释器中有些脚本会被存储在不同的地方,而另一些呢?脚本无法运行。
It's really interesting to me. What's this line? Why this line? How to write this line? Why in such a way?...
这对我来说真的很有趣。这条线是什么?为什么这条线?这行怎么写?为什么要这样?
5 个解决方案
#1
15
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可执行文件位于shell路径中的某个位置时,python才会工作
#!/usr/bin/python
only works if the python binary is actually in /usr/bin
# !只有当python二进制文件实际上位于/usr/bin中时,/usr/bin才可以工作
#!/usr/local/bin/python
also only works if python is in /usr/local/bin
# !/usr/local/bin/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 NOT perl code
# !/usr/bin/perl脚本必须使用perl语法等等……如果您将PHP代码与Perl shebang放在一起,您只需要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系统,在Unix系统中,从来没有真正使用文件扩展名来识别OS的文件类型。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
值得庆幸的是,现在大多数解释器都安装在相当标准的位置上,所以在/some/wonky/weird/path而不是/usr/bin中找到(比如)Perl是有点不寻常的
#2
6
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.
- 读这一行的是译员shebang。如果您编写的python脚本的第一行是“#!”/usr/bin/python“并使用bash调用它,它是/bin/sh解释器,读取第一行并启动适当的解释器。
- 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.
- 一般来说,文件扩展在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.
即使是shebangs也只有在你想要使用$来启动脚本时才需要。/脚本,在这种情况下,你没有提到你想要使用的解释器。
#3
1
- #!/usr/bin/env python
- # !/usr/bin/env python
- issue errors about inconsistent tab usage
- 发布不一致标签使用的错误
- Kernel
- 内核
- #!/path_to_the_interpreter or /usr/bin/env
- # !/ path_to_the_interpreter或/usr/bin/env
- *nix does not check extensinon at all(except some DE could do that)
- *nix根本不检查extensinon(除了一些DE可以这样做)
- This is why you should use #!/usr/bin/env
- 这就是为什么您应该使用#!/usr/bin/env
More info at wiki
更多信息在维基
#4
0
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.
文件存储、调用和使用的不同位置都是基于文件应该位于和由软件定位的已定义位置。设备开发,用户存储区,程序库。但是随着时间的流逝,不同的系统需要不同的位置。
I would suggest getting a book on Linux/Unix and learning the basics of the file system. It does help a lot.
我建议你买一本关于Linux/Unix的书,学习一下文件系统的基础知识。这确实很有帮助。
#5
-1
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是一个命令,用于搜索指定参数的路径,并通过它找到的程序传递脚本。
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.
语法与命令行#!命令参数,但是命令必须是绝对路径,路径不会被搜索。
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脚本。pl脚本。exe,甚至是没有扩展的脚本。如果脚本拥有正确的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在路径中搜索正确的解释器,只要您的系统有/usr/bin/env,您就已经设置好了,您不需要查找或保证解释器的位置。
#1
15
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可执行文件位于shell路径中的某个位置时,python才会工作
#!/usr/bin/python
only works if the python binary is actually in /usr/bin
# !只有当python二进制文件实际上位于/usr/bin中时,/usr/bin才可以工作
#!/usr/local/bin/python
also only works if python is in /usr/local/bin
# !/usr/local/bin/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 NOT perl code
# !/usr/bin/perl脚本必须使用perl语法等等……如果您将PHP代码与Perl shebang放在一起,您只需要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系统,在Unix系统中,从来没有真正使用文件扩展名来识别OS的文件类型。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
值得庆幸的是,现在大多数解释器都安装在相当标准的位置上,所以在/some/wonky/weird/path而不是/usr/bin中找到(比如)Perl是有点不寻常的
#2
6
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.
- 读这一行的是译员shebang。如果您编写的python脚本的第一行是“#!”/usr/bin/python“并使用bash调用它,它是/bin/sh解释器,读取第一行并启动适当的解释器。
- 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.
- 一般来说,文件扩展在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.
即使是shebangs也只有在你想要使用$来启动脚本时才需要。/脚本,在这种情况下,你没有提到你想要使用的解释器。
#3
1
- #!/usr/bin/env python
- # !/usr/bin/env python
- issue errors about inconsistent tab usage
- 发布不一致标签使用的错误
- Kernel
- 内核
- #!/path_to_the_interpreter or /usr/bin/env
- # !/ path_to_the_interpreter或/usr/bin/env
- *nix does not check extensinon at all(except some DE could do that)
- *nix根本不检查extensinon(除了一些DE可以这样做)
- This is why you should use #!/usr/bin/env
- 这就是为什么您应该使用#!/usr/bin/env
More info at wiki
更多信息在维基
#4
0
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.
文件存储、调用和使用的不同位置都是基于文件应该位于和由软件定位的已定义位置。设备开发,用户存储区,程序库。但是随着时间的流逝,不同的系统需要不同的位置。
I would suggest getting a book on Linux/Unix and learning the basics of the file system. It does help a lot.
我建议你买一本关于Linux/Unix的书,学习一下文件系统的基础知识。这确实很有帮助。
#5
-1
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是一个命令,用于搜索指定参数的路径,并通过它找到的程序传递脚本。
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.
语法与命令行#!命令参数,但是命令必须是绝对路径,路径不会被搜索。
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脚本。pl脚本。exe,甚至是没有扩展的脚本。如果脚本拥有正确的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在路径中搜索正确的解释器,只要您的系统有/usr/bin/env,您就已经设置好了,您不需要查找或保证解释器的位置。