💻 Tech
Anything declared without local
is a global variable. In the example below, global_var
and local_var
are printed with expected values with the former a global variable defined outside the function and the latter a local variable defined inside function greet
. another_var
is also a global variable because it doesn’t have local
declaration, that’s why the value is printed outside the function where it was defined. On the other hand, the value of local_var
, a local variable, is not detected.
#!/bin/bash
global_var="ola"
function greet(){
local local_var="hai"
echo $local_var
another_var="salut"
}
echo $global_var
greet
echo $another_var
echo "'" $local_var "'"
➜ ~ ./test1.sh
ola
hai
salut
' '