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

Guy Harris guy at sun.uucp
Tue Apr 30 14:50:47 AEST 1985


> 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:
> 
> 	:
> 
> These two ways are not equivalent, but I can't characterize the
> difference.

The first, on 4.xBSD systems, possibly 2.[89]BSD systems, other systems
which have been modified to support the kernel detecting executable
interpreter scripts (I think Masscomp's system does so), and systems where
the C shell has been modified to recognize "#!" comments, causes the kernel
(in most cases) to execute the program whose pathname is given after the
"#!", with the zeroth argument (i.e., command name) and first argument which
are both the pathname of the script.

The second, on 4.xBSD systems, possibly 2.[89]BSD systems, and on most other
systems running the C shell, causes the C shell to run "/bin/sh" on the
script.  WARNING: beginning with ':' is NOT a necessary condition for being
a Bourne shell script.  The 4.xBSD Bourne shell, and the System III and V
Bourne shells (as well as, I presume, the Korn shell) permit "#" comments
which are real comments, not no-op commands.

> The first sometimes causes commands in the script to fail with the
> error message "restricted"

This is because some versions of the Bourne shell look at their zeroeth
argument (which, in most cases, is their own path name) to see if it
contains the letter 'r' anywhere, or in the better shells only in the last
component of the path name (so being in "/usr/bin" won't confuse them).  If
it does, it considers itself a "restricted" shell and forbids all sorts of
things, like commands with '/' in their name, changing PATH, redirecting
output, etc..

> and produces a funny command line in ps -f:
> 
> 	file file

This is because, as mentioned, both the zeroth and first arguments to
"/bin/sh" are the name of the file.

	Guy Harris



More information about the Comp.unix mailing list