Subject: Re: correct shell behaviour?
To: None <tech-userlevel@netbsd.org>
From: Kevin Schoedel <schoedel@kw.igs.net>
List: tech-userlevel
Date: 08/12/2002 01:28:35
I've just tried the examples on V7 sh, recent commercial UNIX sh, ksh88, and
ksh93 -- the latter three on Solaris 6 and Unixware 7, where each agrees
between platforms.

> (exit 3);echo $? $(echo $?) $?

All agree:
 3 3 0

>echo `echo a; exit 3` >`echo /dev/tty; exit 2` $?

All sh and ksh88 produce
 a 3
but ksh93 produces
 a 0

A quick test shows that ksh93's "0" is the status of the "echo a".
 $ echo `cmp -s /etc/group /etc/group; exit 3` >`echo /dev/tty; exit 2` $?
 0
 $ echo `cmp -s /etc/passwd /etc/group; exit 3` >`echo /dev/tty; exit 2` $?
 1
 $ echo `if cmp -s /etc/passwd /etc/grope; then exit 3; else exit 4; fi` $?
 2
These results are... counterintuitive.

> x=$(echo var >&2) : >/dev/null$(echo redir >&2) $(echo arg >&2)

All but V7 sh agree:
 arg
 var
 redir

V7 doesn't perform the redirection on a ":" (a 'feature'), so it generates
 arg
 var

But wait! If instead of ":" we run an external command, all shells agree:
 arg
 redir
 var

If we run a builtin command, we run away screaming.

Next,

> (exit 2)
> a=`echo a; exit 3`
> echo $?

All agree:
 3

>> The expectation here is that $? refers to the `` pipeline.
>> ie it is possible to test the result of such items.
>
>No, absolutely not.  The last list (pipeline, whatever) is the
>assignment statement.

Apparently not... but for

 $ (exit 2)
 $ a=a
 $ echo $?

all do produce
 0

-- 
Kevin Schoedel
<schoedel@kw.igs.net>
"If I have not seen as far as others,
 it is because I am standing in the footprints of giants."