Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Shebang Line (was: different first line in perl)

by Intrepid (Deacon)
on Sep 29, 2006 at 10:13 UTC ( #575497=note: print w/ replies, xml ) Need Help??


in reply to different first line in perl

greatshots sought to understand that which is described by the following text:

[...] in a very old application called npr [...] I have picked the following snippet from there and ask for your valueable inputs about it. #!`which perl` The above code works fine in Solaris ksh. Is that a appreciatable line in perl ? have anyone of you already used such a line in your perl codes ? can I use those line in my future codes ?

I would say not. It is a thing i don't think i have seen before, but it does something similar to what is done by a much more recent and sound first-line: #! /usr/bin/env perl

Points to understand:

  • This is called "the shebang line" in Perl culture, or alternately the "sharpbang" or the "hash-bang".

  • This is not Perl code. It is not really shell code either. This first line is a means by which, only under Unix systems, a Unix OS kernel can determine which interpreter to run this script under. If the shell becomes involved in this process at all, and I suppose under some very ancient Unix made by some vendor in the benighted depths of history, that could happen, then one might see the shell execute which perl and report back to the kernel what path to use for invoking the Perl interpreter.

  • Non-unix systems like MS Windows do not care about this line, except that the Perl intepreter on those systems sees the line and if there are some switches like -w after the tokens perl, it will take note of them as it would if they had appeared on the commandline.

  • /usr/bin/env perl does them same thing as `which perl` might have done in this obsolete Unix context, on modern Unix systems, without the involvement of the shell.

Caveat: I am not a Unix guru of 30 years experience and I don't warrantee that every aspect of this reply is correct.

If you want the best advice I can give you based on my own understanding, use #! /usr/bin/env perl (but no switches allowed after 'perl'). The space between the ! and the /usr/bin... is deliberate. It caters to the dysfunction of a legacy Unix flavor (I think it is an HPUX) that didn't do correct kernel intepretation of the shebang without the space. It does not harm correct operation of any other known Unix flavor.

    Soren A / somian / perlspinr / Intrepid

-- 
Words can be slippery, so consider who speaks as well as what is said; know as much as you can about the total context of the speaker's participation in a forum over time, before deciding that you fully comprehend the intention behind those words. If in doubt, ask for clarification before you 'flame'.


Comment on Shebang Line (was: different first line in perl)
Select or Download Code
Re: Shebang Line (was: different first line in perl)
by EvanCarroll (Chaplain) on Oct 08, 2006 at 09:07 UTC

    #! is definitly received by the kernel where it undergoes transformation into a normal exec() the reasoning can be found in transcript from irc.freenode.org/#perl code mentioning ->sh_bang found in linux/fs/binfmt_script.c

    ignore this whole post, as it is wrong, and I was wrong
    "This is not Perl code. It is not really shell code either. This first line is a means by which, only under Unix systems, a Unix OS kernel can determine which interpreter to run this script under. If the shell becomes involved in this process at all, and I suppose under some very ancient Unix made by some vendor in the benighted depths of history, that could happen, then one might see the shell execute which perl and report back to the kernel what path to use for invoking the Perl interpreter."
    Actually, it is shell code, taken from `man sh` the default shell on many older systems, to which all shells base the implimentation of #! on
    "If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the pro- gram. The shell executes the specified interpreter on operating systems that do not handle this executable format themselves. The arguments to the interpreter consist of a single optional argument following the interpreter name on the first line of the program, followed by the name of the program, followed by the command arguments, if any."

    Intrepid is sticking to his words. I believe he is wrong, and this is shell specific, and that it tells the shell what interpreter to invoke, his conversation is as follows.

    Intrepid: EvanCarrolll, you're wrong about Re: Shebang Line (was: different first line in perl). Intrepid: tetchy? Intrepid: I'd rather encounter a serene Yellow Giant than a tetchy red dwarf. EvanCarroll: Intrepid according to who, I believe it is you that is wrong EvanCarroll: Intrepid "the kernel" does not parse text, including shebang lines Intrepid: Ah there you are, EvanCarrol. Just /msg'd you. Intrepid: Ooops, apparently spelled you monknick wrong, oh well ;-) EvanCarroll: can we get some more attention at 575497 I would be curios to get this resolved, as I have a downvote from it and i know who it comes from, ;) intrepid. EvanCarroll: Intrepid I still think it is you that is wrong, I even quoted sh's man page Intrepid: You unfortuantely misinterpreted sh' manpage, Intrepid: ... and my writeup had already addressed this issue btw. EvanCarroll: Intrepid I'm not removing my writeup, I'm still convinced you are wrong, Do your homework or cite your source. Intrepid: If you are thinking of taking the d/v personally, EvanCarroll, don't. You picked a bad day to reply to that node; and anyway I famously don't put any credence in XP whatsoever. Intrepid: I'll let time take of it, Evancarroll. As I said, this is old old news, and I'm multitasking a gazillion diff projects. Keep asking around for people to tell you what's correct, if you like.
    Can someone else please take a side/show support etc, or cite a source. Thanks. From freenode.org##linux
    04:29 < EvanCarroll> #! is handeled by the shell right, and has absolutely nothing to do with the kernel 04:29 < EvanCarroll> shebang lines 04:29 < Triffid_Hunter> EvanCarroll: if started by a shell, yes 04:29 < koala_man> it's for the kernel
    freenode.org##perl
    04:20 < EvanCarroll> What is responsible for the shebang interpretation 04:20 < EvanCarroll> the kernel or the shell? 04:20 < EvanCarroll> I thought the shell 04:20 < Dragon22> shell 04:42 < infi> EvanCarroll: I actually think you're wrong on the matter. There is synergy with the '#' being a somewhat normal script comment character, thus a shell ignores the line if you use 'sh foo.sh', but I believe that the '#!' is just a human-readable form of an executable magic number interpreted by execve(2), just like any other ('MZ' for DOS, \177ELF for ELF, etc) 04:44 < EvanCarroll> hrm I could be wrong, I just could have sworn that I read this in begining perl way back when i was first learning, and it is in both sh and bash man page with no mention of the kernel 04:45 < hawkaloogie> EvanCarroll, Unix kernel, not Linux kernel 04:45 < infi> 'magic' is inconclusive about it, as it interprets it as a string, but man 2 execve seems to confirm that. It also seems likely because of the way the 'Kernel support for MISC binaries' feature of the kernel works, via magic number detection associated with an external program. but I haven't found anything conclusive 04:46 < hawkaloogie> last i read it was old, kind of a legacy 04:46 < infi> EvanCarroll: ah, here we go: http://en.wikipedia.org/wiki/Shebang_(Unix) 04:48 < bl0ndie> EvanCarroll prefers the hebang. 04:48 < EvanCarroll> infi: I've had that referenced now like twice where does it mention the kernel 04:48 < EvanCarroll> bl0ndie: cute. bend over. 04:48 < bl0ndie> o.o 04:48 < hawkaloogie> EvanCarroll, read this: http://homepages.cwi.nl/~aeb/std/hashexclam-1.html 04:49 < hawkaloogie> #! was chosen because it wasn't being used as a binary format header yet 04:49 < infi> this seems to agree, too: http://www.in-ulm.de/~mascheck/various/shebang/ 04:50 < infi> EvanCarroll: yeah, I'm hesitant to use wikipedia as an authoritative source, too 04:50 < EvanCarroll> so most definitly it is the kernel 04:50 < EvanCarroll> wow how obscure, I would have never though that in a million years
    . irc.freenode.org/#gentoo
    04:36 < EvanCarroll> What handels the shebang line the kernel or the shell? 04:36 < transient> EvanCarroll: kernel i believe 04:39 < EvanCarroll> Does anyone know for certain if the kernel or shell is responsible for shebang i thought shel.l 04:40 < Slowking_Man> kernel
    . irc.freenode.org/#bash
    04:37 < EvanCarroll> What handels the shebang line the kernel or the shell? 04:37 < twkm> kernel.
    ..


    Evan Carroll
    www.EvanCarroll.com

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://575497]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-15 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (146 votes), past polls