On 2008-07-23, Richard Neill wrote:
> At the moment, variables set within a subshell can never be accessed by 
> the parent script. This is true, even for an implicit subshell such as 
> caused by read.

    The subshell is caused by the ipe, not by read.

> For example, consider the following (slightly contrived example)
>
> ------------
> touch example-file
> ls -l | while read LINE ; do
>       if [[ "$LINE" =~ example-file ]]; then
>               MATCH=true;                             [a]
>               echo "Match-1"
>       fi ;
> done
> if [ "$MATCH" == true ] ;then                         [b]
>       echo "Match-2"
> fi
> ---------------
>
>
> This prints "Match-1", but does not print "Match-2".

touch example-file
ls -l | {
   while read LINE ; do
     if [[ "$LINE" =~ example-file ]]; then
        MATCH=true;
        echo "Match-1"
     fi
   done
   if [ "$MATCH" == true ] ;then
       echo "Match-2"
   fi
}

-- 
   Chris F.A. Johnson, webmaster         <http://Woodbine-Gerrard.com>
   ===================================================================
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

Reply via email to