From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3792 invoked by alias); 6 May 2004 14:13:47 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 3784 invoked from network); 6 May 2004 14:13:46 -0000 Received: from unknown (HELO raven.ecs.soton.ac.uk) (152.78.70.1) by sources.redhat.com with SMTP; 6 May 2004 14:13:46 -0000 Received: from magpie.ecs.soton.ac.uk (magpie.ecs.soton.ac.uk [152.78.68.131]) by raven.ecs.soton.ac.uk (8.12.10/8.12.10) with ESMTP id i46EDj7s013401; Thu, 6 May 2004 15:13:45 +0100 (BST) Received: from ecs.soton.ac.uk (vogue [152.78.66.19]) by magpie.ecs.soton.ac.uk (8.9.3/8.9.3) with ESMTP id PAA24919; Thu, 6 May 2004 15:13:43 +0100 (BST) Message-ID: <409A4817.5010001@ecs.soton.ac.uk> Date: Thu, 06 May 2004 14:20:00 -0000 From: Andy Rushton Organization: Southampton University User-Agent: Mozilla Thunderbird 0.5 (Windows/20040207) MIME-Version: 1.0 To: Kevan Gelling CC: Cygwin mailing list Subject: Re: 1.5.9: Trouble with setting variables using 'read' in a script References: <201BC46BD93D244AB0A910D1203FFC1904B6A873@ecexchange02.euphony.com> In-Reply-To: <201BC46BD93D244AB0A910D1203FFC1904B6A873@ecexchange02.euphony.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-MailScanner-Information: Please contact helpdesk@ecs.soton.ac.uk for more information X-ECS-MailScanner: Found to be clean X-IsSubscribed: yes X-SW-Source: 2004-05/txt/msg00228.txt.bz2 Kevan Gelling wrote: >I'm having trouble setting variables using the 'read' command in bash. > >All of the following lines fail to set $var and return a blank line. > - echo "text" | read var ; echo $var > - cat file | read var ; echo $var > - read var < file | echo $var > > The problem is that the pipeline is executed by spawning a sub-process and running a sub-shell in it. The variable that is being read into is actually a copy that exists within that sub-shell, not the original that you declared in the script. Variables set in sub-shells cannot be passed back to their parent shell (the script itself). >I can get it work by explicitly declaring the file descriptor with the file >redirection, but I'd prefer to use a pipe. > - read -u 0 var > This doesn't create a sub-shell, so the variable being read is the one in the script. >Another related quirk, is that variables set within 'while read' loops lose >their values once the loop ends. The following example displays "text text" >within the loop and blank line outside. > - echo "text" |\ > while read > do > foo=$REPLY ; bar="text" > echo $foo $bar > done > echo $foo $bar > > The while loop also becomes a sub-shell. Same problem as the pipeline. This is a 'feature' of bash and indeed all Bourne-shell derivatives. It makes a lot of shell features much harder to use than you would expect. Incidentally, this is why you cannot write shell scripts to change your environment. The script runs in a sub-shell, changes the local copy of the environment and then discards the local copy on exit leaving the parent shell in the same state as it was in before. Andy -- Andy Rushton, Southampton, UK We may eventually come to realize that chastity is no more a virtue than malnutrition. -- Alex Comfort -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/