On Fri, Jun 1, 2012 at 11:53 AM, freD <frederic.defferr...@gmail.com> wrote: > Configuration Information [Automatically generated, do not change]: > Machine: powerpc > OS: aix5.1 > Compiler: xlc > Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='powerpc' > -DCONF_OSTYPE='aix5.1' -DCONF_MACHTYPE='powerpc-ibm-aix > 5.1' -DCONF_VENDOR='ibm' -DLOCALEDIR='/opt/freeware/share/locale' > -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I > ./include -I./lib -I/opt/freeware/include -O2 > uname output: AIX tsm 1 6 00C530EB4C00 > Machine Type: powerpc-ibm-aix5.1 > > Bash Version: 3.0 > Patch Level: 16 > Release Status: release > > Description: > > In bash mode, variable are lost after a while loop: > > bash-3.00# T=toto ; du | while read a ; do T=$a ; done ; echo $T > toto > > I can keep then a little using parenthesis: > > bash-3.00# T=toto ; du | (while read a ; do T=$a ; done ; echo $T) ; echo $T > 1489648 . > toto > > Repeat-By: > > bash-3.00# T=toto ; du | while read a ; do T=$a ; done ; echo $T > toto > > Fix: > May be starting in "sh" mode and/or posix mode should behave like a > real bourne shell > > bash-3.00# /usr/bin/sh > # T=toto ; du | while read a ; do T=$a ; done ; echo $T > 1489632 . > # exit > bash-3.00# > >
You use a relatively old bash. In the newer bash you can get this beahviour using "shopt -s lastpipe" Note that this behaviour is not mandated by posix and that a least one "real bourne shell", which is not posix acts like bash Eg with the heirloom bourne shell: $ ./sh $ T=toto ; du | while read a ; do T=$a ; done ; echo $T toto I think only ksh93 and zsh behaves like that by default. dash, pdksh, mksh all put the last part of the pipe in a subshell.