Subject: ksh vs. pd-ksh and bash
To: NetBSD Users <netbsd-users@NetBSD.org>
From: Louis Guillaume <lguillaume@berklee.edu>
List: netbsd-users
Date: 03/09/2007 16:49:40
Hi,
I'm trying to figure out a shell problem so that the script is portable.
bash and pdksh seem to behave the same, but I think they're wrong. They
seem to run the `while' segment of the pipeline in a subshell. Or something.
ksh88 does the right thing.
Is there some good reason for bash's and pdksh's misbehaviour??
Louis
### ksh Version M-11/16/88f (AIX 5.3) ###
ksh $ x=0
ksh $ cat <<END | while read i ;do echo "x = $x"; (( x += i )); echo
"new x = $x"; done
> 1
> 2
> 3
> END
x = 0
new x = 1
x = 1
new x = 3
x = 3
new x = 6
ksh $ echo $x
6
ksh $
### PD KSH v5.2.14 99/07/13.2 ###
pdksh $ cat <<END | while read i ;do echo "x = $x"; (( x += i )); echo
"new x = $x"; done
> 1
> 2
> 3
> END
x = 0
new x = 1
x = 1
new x = 3
x = 3
new x = 6
pdksh $ echo $x
0
pdksh $
### GNU bash, version 3.2.1(1)-release (i386--netbsdelf) ###
bash $ x=0
bash $ cat <<END | while read i ;do echo "x = $x"; (( x += i )); echo
"new x = $x"; done
> 1
> 2
> 3
> END
x = 0
new x = 1
x = 1
new x = 3
x = 3
new x = 6
bash $ echo $x
0
bash $