Horje

Bash - global and local variables
Start
  Global variables
var="hello"

function foo(){
    echo $var
}

foo

Will obviously output "hello", but this works the other way around too:

function foo()  {
    var="hello"
}

foo
echo $var

Will also output "hello"

  global and local variables

By default, every variable in bash is global to every function, script and even the outside shell if you are declaring your variables inside a script.

If you want your variable to be local to a function, you can use local to have that variable a new variable that is independent to the global scope and whose value will only be accessible inside that function.

  Mixing the two together
var="hello"

function foo(){
    local var="sup?"
    echo "inside function, var=$var"
}

foo
echo "outside function, var=$var"

Will output

inside function, var=sup?
outside function, var=hello


Read Article
https://develop.horje.com/learn/1434/reference