"#! /bin/sh" vs ":"

Thomas M. Breuel tmb at talcott.UUCP
Mon Apr 29 11:19:24 AEST 1985


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



More information about the Comp.unix mailing list