gnu.bash.bug
[Top] [All Lists]

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

Subject: bash: request for a way to return variables to the parent of a subshell
From: Richard Neill <rn214@xxxxxxxxxxxxxxxx>
Date: Wed, 23 Jul 2008 03:04:34 +0100
Newsgroups: gnu.bash.bug


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.

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".

The only way to get data out of the read-subshell is by something like "exit 2", and looking at $?




It's already possible to export a variable into the environment, and for subshells to inherit variables from the main script. Do we need a new keyword to achieve the reverse? Is there any way to make sure that variables defined at [a] can be made to still exist at [b] ?


Thanks,

Richard





<Prev in Thread] Current Thread [Next in Thread>