Skip to content

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.

Terminal window
var="hello"
function foo(){
echo $var
}
foo

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

Terminal window
function foo() {
var="hello"
}
foo
echo $var

Will also output "hello"

Terminal window
function foo() {
local var
var="hello"
}
foo
echo $var

Will output nothing, as var is a variable local to the function foo, and its value is not visible from outside of it.

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

Will output

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