in reply to #! -- why doth thou exist?

Sometimes you get more power from UNIX than you expect, as Tanktalus suggests. The shell can be your best friend, but it can obtusely bite you as well. Programs can be written for many interpreters (including many shells and languages), and you mostly do not want to be required to call the interpreter first; you want to 'execute' the file directly. The two bytes \x23\x21 are called the 'magic number' that signifies that the rest of that line is a path to the proper interpreter when they are the first two bytes of the file. However, a direct command line input is interpreted as though it is "$quoted", but the #! line is not. If the interpreter (perl) is already handling the file, the #! looks like a comment.

There are many reasons why #! exists. You might wish to use a statically-compiled version of a language, or a 64-bit version, for some things and not for others.

Ah, the power of source code: You could even compile a Perl that reads the #! and replaces itself with the interpreter specified there! :D

Replies are listed 'Best First'.
Re^2: #! -- why doth thou exist?
by TilRMan (Friar) on Aug 26, 2005 at 03:07 UTC
    You could even compile a Perl that reads the #! and replaces itself with the interpreter specified there!

    Indeed, perl does this out of the box.

    $ cat foo.sh #!/bin/sh echo 'Hello, World!' $ perl foo.sh Hello, World!

    perlrun has the details, though I prefer Dominus' take in perliaq.

      But, as perlrun says, that trick doesn't work when the interpreter has "perl" in the name, even if it's a different version. That is,
      $ cat foo.pl #!/usr/bin/perl print "$]\n"; $ ~/bin/perl foo.pl

      will run ~/bin/perl, and not /usr/bin/perl. I think this is good more often than not.