bash while read question

Evuraan evuraan at gmail.com
Thu May 6 06:41:36 UTC 2010


2010/5/5 CyberLeo Kitsana <cyberleo at 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

>


More information about the freebsd-questions mailing list