Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Shebang (space?) "#! "?

Tags:

unix

sh

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.

like image 664
vol7ron Avatar asked Apr 17 '12 19:04

vol7ron


2 Answers

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

like image 118
Keith Thompson Avatar answered Oct 17 '22 05:10

Keith Thompson


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

like image 32
leonbloy Avatar answered Oct 17 '22 04:10

leonbloy