Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/3/84; site talcott.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!harvard!talcott!tmb From: tmb@talcott.UUCP (Thomas M. Breuel) Newsgroups: net.unix,net.unix-wizards Subject: Re: "#! /bin/sh" vs ":" Message-ID: <422@talcott.UUCP> Date: Sun, 28-Apr-85 21:19:24 EDT Article-I.D.: talcott.422 Posted: Sun Apr 28 21:19:24 1985 Date-Received: Tue, 30-Apr-85 03:31:28 EDT References: <183@ski.UUCP> Organization: Harvard University Lines: 26 Xref: linus net.unix:3749 net.unix-wizards:10138 [the following is valid for Berkley kernels 2.8 and up and 4.1 and up] > There are at least 2 ways of indicating to csh that a Shell script > should be interpreted by sh: begin the file with: > #! /bin/sh > or with any character other than "#", such as: > : Every executable begins with a magic number which identifies the type of the executable. '#!' is a magic number which indicates that a command interpreter should be invoked by the kernel (!) to execute this file. Sample uses are to invoke a shell on a shell file, or to invoke a Pascal p-code interpreter on a p-code file. Files beginning with '#!' can be exec'ed, they can even be suid (although that's not a good idea). The name of the file being executed is passed as a parameter to the command interpreter. On 4.1 and up, one (1) more argument can be handed to the command interpreter (e.g. a flag). Files beginning with '#!' behave practically like binary files. The shell just exec's them and never sees them. If, however, the exec fails (because the file to be executed has an invalid magic number), the shell (sh or csh) looks at the first character of the file. If it is a '#', it invokes a 'csh' to interpret it, otherwises an 'sh'. (Note that the 'csh' is the special case, not the 'sh'). Thomas.