which/type & built-ins : A question ?

Chet Ramey chet at cwns1.CWRU.EDU
Tue Jan 9 02:39:21 AEST 1990


I wrote:

>> Because `while' is a sh language construct (a statement), not a shell
>> builtin.

and Kirtikumar Satam asks:

>Can you elaborate more on this??

This is long and rambling (and not particularly clear), but I hope it
catches the basic distinction between shell statements and builtins.

The shell is, in effect, a complete interpreted programming language.  In
that language, there are statements provided as in any programming
language, mostly looping and alternation constructs.  Sh actually parses
each complete statement in its input into a parse tree (sh uses a recursive
descent parser, bash uses a yacc-generated parser), and executes it. 
While, if, for, until, and case statements are all sh language constructs. 
Shell builtins are parsed as any other command word would be; the fact that
they are shell builtins is not discovered until the parse tree is executed. 
The C shell has everything as builtins, and uses ad-hoc parsing mechanisms
to implement its `language constructs', with lots of special cases (which is
why, for instance, it is so particular about the appearance of white space
in its `if' statement).

if [ -t 0 ] ; then
	echo terminal
else
	echo no terminal
fi

is one sh statement that is parsed into a tree and executed.  `if' is
executed as a builtin by csh, and the if builtin is responsible for
consuming as much input as it needs.  This is one reason redirection in the
C shell is so limited, like redirections into and out of loops.  In sh, a
statement is always completely parsed before it is executed, so
redirections into and out of loops are trivial to implement. 

Chet Ramey

-- 
Chet Ramey
Network Services Group				"Help! Help! I'm being
Case Western Reserve University			 repressed!"
chet at ins.CWRU.Edu			



More information about the Comp.unix.questions mailing list