bug-bash
[Top][All Lists]
Advanced

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

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


From: Richard Neill
Subject: bash: request for a way to return variables to the parent of a subshell
Date: Wed, 23 Jul 2008 03:04:34 +0100
User-agent: Thunderbird 2.0.0.14 (X11/20080629)

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






reply via email to

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