problems with 'while read'

Leslie Mikesell les at chinet.chi.il.us
Tue May 7 13:16:52 AEST 1991


In article <3635 at dagobert.informatik.uni-kiel.dbp.de> wib at informatik.uni-kiel.dbp.de (Willi Burmeister) writes:

>while read num
>do
>  echo "do something with <$num>"
>  last=$num
>done < hugo
>
>echo "last num = <$last>"

This creates a subshell to run the redirected loop. Thus, outside of
the loop, variables are unaffected.  If no other redirections
are needed, just redirect the whole script.  If they are, you may
be able to use parens to force the subshell to contain the part that
needs the affected variable:

(
while read num
do
  echo "do something with <$num>"
  last=$num
done
echo "last num = <$last>"
) < hugo

Les Mikesell
  les at chinet.chi.il.us



More information about the Comp.unix.shell mailing list