问题描述
我都看过:
#!/path/...#!/小路/...
怎么了?有关系吗?有历史吗?
我听说一个古老的 Unix 版本要求没有空格.但后来我听说那只是谣言.有人确切知道吗?
我想不出在哪里问这个更好.据我所知,它与编程有关,因为空间可以使程序以不同的方式运行.所以我在这里问.
我也有一个模糊的记忆,在一些旧的类 Unix 系统中不允许使用空格,但一些研究不支持这一点.
根据这篇维基百科文章,#!
语法于 1980 年 1 月在第 8 版 Unix 中引入.丹尼斯·里奇 (Dennis Ritchie) 最初宣布此功能时说:
系统已更改,如果正在执行的文件开始使用魔术字符 #!
,该行的其余部分被理解为是已执行文件的解释器的名称.以前(和实际上仍然)外壳完成了大部分工作;它自动在文本文件中以可执行模式在文本文件上执行自身文件名是作为命令输入的.将设施放入系统具有以下优点.
[剪辑]
为了利用这个绝好的机会,把
#!/bin/sh
在 shell 脚本第一行的左边距.空白!
之后就OK了.使用完整的路径名(不进行搜索).在整行被限制为 16 个字符,但这个限制将被提升.
可以想象,一些后来的类 Unix 系统支持 #!
语法但不允许 !
后面有空格,但考虑到第一个实现明确允许空白,这似乎不太可能.
leonbloy 的回答提供了更多背景信息.
更新:
Perl 解释器本身识别以 #!
开头的一行,即使在内核无法识别的系统上也是如此.运行 perldoc perlrun
或查看此网页了解详情.>
#!当线路正在运行时,始终检查线路是否有开关解析.因此,如果你在一台只允许一个参数的机器上与 #!行,或者更糟的是,甚至无法识别 #!线,你不管 Perl 是怎样的,仍然可以获得一致的切换行为调用,即使 -x 用于查找程序的开头.
Perl 也允许在 #!
之后有空格.
(就我个人而言,我更喜欢编写没有空格的 #!
行,但无论哪种方式都可以.)
leonjoy 的回答指向此网页由 Sven Mascheck 提供,其中讨论了 #!深入.(我现在提到这个是因为最近关于 comp.unix.shell 的讨论.)
I've seen both:
#!/path/...
#! /path/...
What's right? Does it matter? Is there a history?
I've heard that an ancient version of Unix required there not be a space. But then I heard that was just a rumor. Does anyone know for certain?
Edit: I couldn't think where better to ask this. It is programming related, since the space could make the program operate in a different way, for all I know. Thus I asked it here.
I also have a vague memory that whitespace was not allowed in some old Unix-like systems, but a bit of research doesn't support that.
According to this Wikipedia article, the #!
syntax was introduced in Version 8 Unix in January, 1980. Dennis Ritchie's initial announcement of this feature says:
[SNIP]
It's conceivable that some later Unix-like system supported the #!
syntax but didn't allow blanks after the !
, but given that the very first implementation explicitly allowed blanks, that seems unlikely.
leonbloy's answer provides some more context.
UPDATE :
The Perl interpreter itself recognizes a line starting with #!
, even on systems where that's not recognized by the kernel. Run perldoc perlrun
or see this web page for details.
Perl also permits whitespace after the #!
.
(Personally, I prefer to write the #!
line without whitespace, but it will work either way.)
And leonjoy's answer points to this web page by Sven Mascheck, which discusses the history of #! in depth. (I mention this now because of a recent discussion on comp.unix.shell.)
这篇关于Perl:Shebang(空格?)"#!"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!