bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash: request for a way to return variables to the parent of a subsh


From: Chris F.A. Johnson
Subject: Re: bash: request for a way to return variables to the parent of a subshell
Date: Wed, 23 Jul 2008 01:47:09 +0000
User-agent: slrn/0.9.8.1 (Linux)

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

[Prev in Thread] Current Thread [Next in Thread]