Global variables, and their scope in Bash.

Ray Parrish crp at cmc.net
Mon Dec 14 00:59:59 UTC 2009


Lachlan Musicman wrote:
> On Mon, Dec 14, 2009 at 11:22, Ray Parrish <crp at cmc.net> wrote:
>   
>> Hello,
>>
>> I have a global variable called LoopCount, that I want to access, and
>> write to in four different functions. The functions are as follows, [not
>> actual code, just showing function names] -
>>
>> ReadintoArray [
>>     # Loop with LoopCount, then reset it to zero.
>> }
>> WriteTopSection {
>>     # Loop with LoopCount, then decrement it one.
>> }
>>     
> .....
>   
>> Then perversely the ending value of LoopCount does not get carried
>> forward from WriteTopSection to WriteDescriptionSection, but instead is
>> once again at the value of zero.
>>     
>
>   
> that would be because you are decrementing by 1 as you loop LoopCount times?
>   
Sorry, I was a little unclear there... It actually increments up to a 
certain value, then decrements only one *after* the loop so that the 
next section can have the proper value to carry on through an array from 
that point.

I cannot understand why ReadintoArray can affect it's value globally, 
but WriteTopSection cannot. It's always set to 0 when 
WriteDescriptionSection starts, but should be at around 15 or so.

Later, Ray Parrish
>   
>> Why in the world can my ReadintoArray function modify the value of the
>> global variable LoopCount, but the next function down cannot?
>>
>> Does anyone have any light they can shed on the behavior of global
>> variables in Bash?
>>
>> Thanks, Ray Parrish
>>
>>
>> The Future of Technology.
>> http://www.rayslinks.com/The%20Future%20of%20Technology.html
>> Ray's Links, a variety of links to usefull things, and articles by Ray.
>> http://www.rayslinks.com
>> Writings of "The" Schizophrenic, what it's like to be a schizo, and other
>> things, including my poetry.
>> http://www.writingsoftheschizophrenic.com
>>
>>
>>
>> --
>> ubuntu-users mailing list
>> ubuntu-users at lists.ubuntu.com
>> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>>
>>     
>
>
>
>   


-- 
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list