Subject: appending to a shell variable inside a loop
To: None <netbsd-help@netbsd.org>
From: None <brook@biology.nmsu.edu>
List: netbsd-help
Date: 03/11/2004 07:58:15
I am trying to write a shell script that will construct a list of
words while inside a loop. Here is the logic I would have expected to
work:
#!/bin/sh
LIST=""
cat - | while true
do
read WORD JUNK
if [ $? -ne 0 ]; then break; fi
LIST="$LIST $WORD"
echo "LIST: $LIST"
done
echo "final LIST: $LIST"
The problem is that the final value of $LIST is empty, though within
the loop the value grows as expected. That is, the assignment inside
the loop seems to have no effect on the outermost $LIST. Adding
export does not seem to matter.
I must be missing something obvious about shell variables. How can
this be done?
Thanks for your help, and sorry for what must be something simple that
I'm completely missing.
Cheers,
Brook