X-Spam-Check-By: sourceware.org Message-Id: <1161952728.23693.274366024@webmail.messagingengine.com> From: cwmail AT allmail DOT net To: "Dave Korn" Cc: cygwin AT cygwin DOT com Content-Disposition: inline Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="ISO-8859-1" MIME-Version: 1.0 X-Mailer: MessagingEngine.com Webmail Interface References: <01e301c6f9b3$26422890$a501a8c0 AT CAM DOT ARTIMI DOT COM> Subject: RE: Piping to the 'read' command In-Reply-To: <01e301c6f9b3$26422890$a501a8c0@CAM.ARTIMI.COM> Date: Fri, 27 Oct 2006 20:38:48 +0800 X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Thanks Dave - I guess I should have explained in my original email that I'm converting a ksh script which currently runs successfully under HP-UX. If I run the following command under HP-UX ksh: echo "Test" | read VAR1 VAR1 will hold the value "Test" in the parent environment. This doesn't happen under Cygwin pdksh, and I don't understand why. Regards Ian ----- Original message ----- From: "Dave Korn" To: cygwin Date: Fri, 27 Oct 2006 11:32:06 +0100 Subject: RE: Piping to the 'read' command On 27 October 2006 04:04, cwmail AT allmail DOT net wrote: > Can anyone explain what is happening here (using pdksh as my shell) when > I try to set an environment variable using 'read': Variables set in a subshell process are not returned back into the parent process environment. > > This works: > $ read VAR1 > Test1 > $ echo "VAR1 is $VAR1" > VAR1 is Test1 VAR1 exists in the main pdksh process. It is set by read and displayed by echo. > > This doesn't work: > $ echo Test2 | read VAR2 > $ echo "VAR2 is $VAR2" > VAR2 is The second process on the end of the pipe is a subshell. VAR2 is set in the subshell. When the command terminates the subshell exits, VAR2 is lost, and you are left at the command prompt in the parent shell, which is not where VAR2 was set. To see the value of VAR2 in the subshell, combine the echo with the read like so: \u@\h \w> echo Test2 | ( read VAR2 ; echo "VAR2 is $VAR2" ) VAR2 is Test2 > This works within the 'while' loop only: > $ echo Test3 | while read VAR3 >> do >> echo "VAR3 is $VAR3" >> done > VAR3 is Test3 > $ echo "VAR3 is $VAR3" > VAR3 is > The body of the loop is the subshell process. VAR3 again exists only within the subshell where it is set. cheers, DaveK -- Can't think of a witty .sigline today.... -- 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/ -- 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/