#! is the "magic number" for interpreted languages. The kernel looks at your file that you're trying to run, and determines what "mode" to run it in. (Some OS's only support a single binary type, but many support multiple - e.g., ELF and libc formats on Linux, or Win32, Win16, and OS/2-16 on Windows, etc.) One of these modes, on unix systems anyway, is the "interpreted" mode where the kernel reads that first line, and calls that executable on the file instead.

So, when you run "./test.pl", the kernel looks at your file and sees the #! magic number, and then reads the line, sees to run "/Users/punkish/bin/perl ./test.pl", and does so. In turn, it looks at /Users/punkish/bin/perl, sees that its magic number is a binary native to MacOSX, knows how to run it, and does so.

However, the kernel does not know what ~ is - that's something your shell knows how to interpret. If you're on the commandline, the shell replaces that character before passing it to the kernel, while if it's in the #! line, the kernel doesn't know how to deal with it, and aborts running the file.

Welcome to unix :-)


In reply to Re: #! -- why doth thou exist? by Tanktalus
in thread #! -- why doth thou exist? by punkish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.