Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Jan 2000 22:40:08 +0100 (CET)
From:      Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de>
To:        freebsd-questions@FreeBSD.ORG, cjclark@home.com
Subject:   Re: sh(1) Messing with My Mind
Message-ID:  <200001202140.WAA05161@dorifer.heim3.tu-clausthal.de>
In-Reply-To: <867h6j$1kk4$1@atlantis.rz.tu-clausthal.de>

next in thread | previous in thread | raw e-mail | index | archive | help
Crist J. Clark <cjc@cc942873-a.ewndsr1.nj.home.com> wrote in list.freebsd-questions:
 > [...]
 > seems to have muddled my thoughts this morning. Why does this happen:
 > 
 > $ echo 3 | read NUM
 > $ echo $NUM  
 > 

Because the read command is executed in a subshell when it is
in a pipe.  When the pipe ends, the subshell terminates, and
its environment variables are gone.

 > [...]  Why does it look like
 > read won't read from a piped stdin?

It does read from the pipe, but the variable is only set in the
subshell.  It does not change the environment of the parent
shell process.

You usually solve this problem by using command substitution
("backticks"):

   NUM=`echo 3`

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:olli@dorifer.heim3.tu-clausthal.de)

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
                                         (Terry Pratchett)


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200001202140.WAA05161>