bash while read question

CyberLeo Kitsana cyberleo at cyberleo.net
Thu May 6 02:34:26 UTC 2010


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.

-- 
Fuzzy love,
-CyberLeo
Technical Administrator
CyberLeo.Net Webhosting
http://www.CyberLeo.Net
<CyberLeo at CyberLeo.Net>

Furry Peace! - http://wwww.fur.com/peace/


More information about the freebsd-questions mailing list