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

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


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

Tags:

Unix

Sh