How to call variable inside a function globally?

Hi Gurus,

Is there a way to call a variable inside a function anywhere within the script?

Thanks.

BR,
Ernesto

You need a variable with global scope:

#!/bin/bash
# this variable, foo, has global scope
# it has to be defined in the code before the function
foo=1

func()
{
   a=$(( foo +1 ))
   foo=$a
}

# main code block
echo "before function foo = $foo"
func
echo "after function foo = $foo"
exit
1 Like

Jim, why didn't you start from 0 ? :slight_smile: