Perl: Shebang (space?) “#! ”?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 08:02:11

问题


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.


回答1:


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:

The system has been changed so that if a file being executed begins with the magic characters #!, the rest of the line is understood to be the name of an interpreter for the executed file. Previously (and in fact still) the shell did much of this job; it automatically executed itself on a text file with executable mode when the text file's name was typed as a command. Putting the facility into the system gives the following benefits.

[SNIP]

To take advantage of this wonderful opportunity, put

#! /bin/sh

at the left margin of the first line of your shell scripts. Blanks after ! are OK. Use a complete pathname (no search is done). At the moment the whole line is restricted to 16 characters but this limit will be raised.

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.

The #! line is always examined for switches as the line is being parsed. Thus, if you're on a machine that allows only one argument with the #! line, or worse, doesn't even recognize the #! line, you still can get consistent switch behaviour regardless of how Perl was invoked, even if -x was used to find the beginning of the program.

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.)




回答2:


It seems to usually work both ways. See here. I'd say that the no-space version is much more common today, and, to me, much more appealing.

BTW, this is not specifically related to Perl (but it's definitely related to programming).



来源:https://stackoverflow.com/questions/10197690/perl-shebang-space

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!