variable scope in bash

From: e-doug (doug_at_forguson.com)
Date: 08/29/04


Date: Sat, 28 Aug 2004 23:38:54 -0400

redirecting the output from a loop seems to disrupt the scope of a variable
under bash.

#!/bin/bash

b=0

while [ $b -lt 10 ]
do
let "b++"
done

echo $b

produces the expecte dout of 10 - so far so good

now....

#!/bin/bash

b=0

while [ $b -lt 10 ]
do
let "b++"
done | sort

echo $b

results in the output of '0'

It acts as though the redirect causes a subshell where the local variable
isn't shared.

Comments?



Relevant Pages

  • Re: time question
    ... I tried redirecting it ... bash doesn't recognize this switch. ... maybe u r using hpux, under linux, time is a shell keyword ... default shell in hpux is not bash or ksh ...
    (comp.unix.shell)
  • Re: Is it possible to make system use bash instead of sh?
    ... Does anyone know if it is possible to use bash instead of sh as the ... Others have indicated how you can execute the command with bash rather than sh. ... I'm interested in why you have problems with redirecting standard ... time since I did java, but from memory, it uses quite a few shell wrapper ...
    (comp.lang.ruby)