Link Search Menu Expand Document

Variable

#! /bin/bash 

myFunction(){
    var1="Eggs"
    var2="Salsa"
    echo "my variable inside the function is: $var1"
}

echo "start here ..."
myFunction
echo "outside: $var1"
#all above will output $var1
#every variable in the bash script is global unless you define with local

myFunction2(){
    local var1="Eggs"
    var2="Salsa"
    echo "my variable inside the function is: $var1"
}

echo "start here ..."
myFunction 
#this will output 
echo "outside: $var1" 
#this variable will not output as it's defined as local variable inside the function 
echo "outside: $var2" 
# $var2 is a global variable