Line at a time scripts in csh

Guy Harris guy at auspex.UUCP
Fri Jan 27 19:00:52 AEST 1989


>Why was a "sh" child produced, instead of another "csh" (or, anyway,
>a "ksh", which is my login shell, and the value of SHELL)? Any ideas?

A long time ago, in a galaxy far, far away (well, I dunno, how far is
Berkeley, CA from Napierville, IL?), Bill Joy wrote a shell for either
V6 or V7 UNIX.  At least when run under V7, it wasn't compatible with
the existing UNIX shell; since people wanted to use the C shell as their
login shell, and possibly write C shell scripts, but still be able to
run Bourne shell scripts as well, both the Bourne and C shells were
hacked up to try to recognize each other's scripts and run the other
shell if they recognized a "foreign" script.

The versions of the Bourne and C shells that I'm familar with that do
this check the first character of the script; the C shell assumes that a
script beginning with ":" is a Bourne shell script, and the Bourne shell
assumes that a script beginning with "#" is a C-shell script.  (":" was
a no-op command used as a sort of comment in the Bourne shell; the C
shell accepted comments beginning with "#" which were *real* comments
not scanned by its parser, so ":" tended not to be used in C shell
scripts, and since the Bourne shell did not originally have "#"
comments, "#" tended not to be used in Bourne shell scripts.)

(Subsequently, Berkeley added the "#!" mechanism - suggested, I'm told,
by Dennis Ritchie - that allows "exec" system calls to "directly"
execute shell scripts; the kernel recognizes "#!" as the first two
characters of a file it's told to execute, and sucks a pathname from the
first line of the file and runs the program with that pathname, handing
it the pathname of the file it was originally told to execute as an
argument.  This obviates the need for the Bourne/C shell hacks, and just
in time - more recent Bourne shells support "#" comments, so you can't
validly use "#" as the first character as the deciding factor any more.)

>Parenthetically, I did the same sort of thing with "ksh" instead of "csh",
>and another "ksh" (NOT a "sh") was forked to run the "ps".

Yup, "ksh" basically thinks of itself as the only shell in the world (as
did the original Bourne shell), and if it tries to "exec" a program and
fails because it's not an executable image, it kicks off another "ksh"
to run it.  ("#!" allows you to mark shells as Bourne, C, or Korn shell
scripts - or as "awk" scripts, or....)



More information about the Comp.unix.questions mailing list