Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 May 2010 23:41:29 -0700
From:      Evuraan <evuraan@gmail.com>
To:        freebsd-questions@freebsd.org
Subject:   RE: bash while read question
Message-ID:  <h2q6fbcd0711005052341tdd558209j3c3dfcd7dff12108@mail.gmail.com>
In-Reply-To: <h2x6fbcd0711005052332g79d711adpd5a3caeed2ce80f6@mail.gmail.com>
References:  <x2n6fbcd0711005051825mf0835e29jbf5daaab5111b336@mail.gmail.com> <4BE22AAF.8020608@cyberleo.net> <h2x6fbcd0711005052332g79d711adpd5a3caeed2ce80f6@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
2010/5/5 CyberLeo Kitsana <cyberleo@cyberleo.net>:
> On 05/05/2010 08:25 PM, Evuraan wrote:
>> I cant figure out why the variable in in loop2 does not hike to +1?
>> (its a friday, i am dazed, I admit. but this should not be a mystery!)
>> any help would be much appreciated.
>> <snip>
>> $ cat loop2
>> #! /bin/bash
>>
>> date > /tmp/somefile
>> b="1"
>> cat /tmp/somefile | while read blah; do
>> let b=(b+1)
>> done
>> echo "variable is $b"
>
> This particular syntax executes the 'while' block in a subshell. The
> variables set or altered in the subshell are never propagated back up to
> the parent shell.
>
duh, i get it now, anytime stuff is piped , a subshell is evoked:

http://tldp.org/LDP/abs/html/subshells.html says,

<snip>
Redirecting I/O to a subshell uses the "|" pipe operator, as in ls -al
| (command).
</snip>

thanks for the "reset"..!

> --
> Fuzzy love,
> -CyberLeo

>



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