On 09:09 02 Dec 2002, Ira Childress <[EMAIL PROTECTED]> wrote: | I'm sure some one has seen this before and hopefully there's a work-around. | | houston:/admin/IrasDir # cat wtest | #!/bin/bash | # | myVar="Old Value" | | echo "1: "$myVar | | echo "New Value" | while read data ; do | myVar=$data | echo "2: "$myVar | done | | echo "3: "$myVar | | returns, | | 1: Old Value | 2: New Value | 3: Old Value | | In otherwords, myVar gets changed inside the while loop, but doesn't retain | the value when the loop exits.
Sure. The assignment to myVar is happening in the subshell use for the loop (because it's a component in the pipeline). There are a few approaches, which tend to fall into either: not doing the loop in a subshell doing the loop in a subshell and manually making what you want available later putting the code wanting $myVar also in the subshell eg: First method: echo "New Value" >tempfile while read data do myVar=$data done <tempfile Which will be run my the main shell because it's not in the pipeline. Second method: echo "New value" | while read data do myVar=$data echo $myVar >tempfile done myVar=`cat tempfile` Which writes to a tempfile, and the main shell reads from it after the pipeline finishes. Third method (which is what I often use): echo "New Value" | \ { while read data do myVar=$data done echo $myVar } Which puts the post-loop stuff wanting $myVar in the subshell that is part of the pipeline. Cheers, -- Cameron Simpson, DoD#743 [EMAIL PROTECTED] http://www.zip.com.au/~cs/ Ed Campbell's <[EMAIL PROTECTED]> pointers for long trips: 1. lay out the bare minimum of stuff that you need to take with you, then put at least half of it back. -- redhat-list mailing list unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe https://listman.redhat.com/mailman/listinfo/redhat-list