csh misconceptions

gwyn%brl-vld at sri-unix.UUCP gwyn%brl-vld at sri-unix.UUCP
Sat Mar 3 15:22:24 AEST 1984


From:      Doug Gwyn (VLD/VMB) <gwyn at brl-vld>

Thanks for adding more misinformation to the list.

The 4.2BSD Cshell, assuming it has been compiled with OTHERSH defined
as "/bin/sh" as is normally the case, will do the following when you
attempt to execute a shell script (or anything with execute permission
that does not have a "magic number" flagging it as an executable
machine-code binary image):

(1)  The kernel is asked to execv() it.  One of two things can happen:

	(a)  If the script starts with #!progname etc. then the kernel
	arranges for the script to be attached to the standard input of
	the specified program, typically /bin/sh or /bin/csh, which
	takes it from there.

	(b)  Otherwise, the execv() returns to the Cshell with an
	ENOEXEC error indication.

(2)  If there is an alias defined for "shell", then the Cshell will
prepend that to the command and try another execv().

(3)  Otherwise, if the first character of the script is a # character,
the shell specified in the "shell" environment variable is prepended,
or if there is none specified "/bin/csh" is used.  This would usually
be /bin/csh for most Cshell users.  The resulting command is execv()ed.

(4)  Otherwise, OTHERSH (normally /bin/sh) is prepended and execv()ed.

>From the above, it is clear that the ONLY way to ensure that a Bourne
shell script is interpreted by /bin/sh as intended is to supply the
first line

	#!/bin/sh

as I originally said.

Item (3) is pretty silly since most Bourne shell scripts these days
start with a # comment line.

The # comment has been in the Bourne shell since at least 1980.  The
: form is NOT A COMMENT although people used it as one since there
was no true comment until # was added to the shell syntax.  The 6th
Edition UNIX shell used : at the beginning of a line followed by a
label to support "goto label", but it didn't take long for people to
realize that the label could actually be a comment instead.  The
trouble with this is that the Bourne shell EVALUATES the line after
the : command, so one could get "syntax errors" and so forth unless
the comment were quoted.  (Incidentally, the : command does nothing
and returns a 0 exit code, so
	while	:
	do	..stuff..
	done
is a fast way to do a "forever" loop.)

The Bourne shells shipped with 4.2BSD, UNIX System III, and UNIX
System V all support # comments.  Anyone with an older version has a
pretty buggy shell and should upgrade anyway.

...That's what I get for trying to give brief advice instead of a
lengthy treatise...



More information about the Comp.unix mailing list